Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33273 closed Bug (needsinfo)

queryset.update SQL does not receive fields from base model of inheriting model.

Reported by: James Miller Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by James Miller)

In an admin function, I receive a queryset with the class that was registered with the admin decorator, @admin.register(ForumComment), where the queryset is populated with ForumComments selected to approve, where they have been reported for moderation.

In my approve_comment function, I use queryset.update(moderation=None). The ForumComment model extends from a Comment model in another app. When the code 'queryset.update(moderation=None) runs I get the following error:
`
* django.db.utils.OperationalError: (1054, "Unknown column 'django_posts_and_comments_comment.date_created' in 'order clause'"
`

It seems that the queryset.update method is unable to scope the model 'ForumComment' to correctly include the methods/properties of the superclass 'Comment'.

I have just returned to the code base after a while away, and I think that it worked, and the error is new. I have solved the error by iterating over the records of the queryset and saving each one individually using update_fields for efficiency, but the queryset.update should work.

I have asked this question at the forum where I have included code etc.
https://forum.djangoproject.com/t/unknown-field-in-order-clause-when-using-model-inheritance/10444/2

In sum, should it not be the case that queryset.update is capable of generating SQL that will correctly include the fields of a superclass model?

Change History (6)

comment:1 by James Miller, 3 years ago

Description: modified (diff)

comment:2 by Carlton Gibson, 3 years ago

I think that it worked, and the error is new

Are you able to use git bisect to find where the error was introduced in this case?

comment:3 by Carlton Gibson, 3 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: needsinfo
Status: newclosed
Type: UncategorizedBug

Hi James.

From the description here and on the forum thread, I can't see whether this is a usage question, or expected behaviour, or a bug in Django.
If you could reduce it to a minimal sample project and upload that here, I'm happy to have a further look.
Thanks!

comment:4 by James Miller, 3 years ago

Hi, thanks for the response. I am not sure I could use git bisect, as I would have to bisect the django source code, and the error occurs in my own code. I haven't used git bisect before.
I can make a sample project, so I will do that now.

comment:5 by Carlton Gibson, 3 years ago

Super. Thanks James, it makes it much easier to investigate! 👍 Please reopen the ticket when you upload the project.

Last edited 3 years ago by Carlton Gibson (previous) (diff)

comment:6 by James Miller, 3 years ago

Ok, thanks, I found the problem. In the inheriting app's model class Meta: I had an 'ordering' field, that referenced the base class 'date_created' field. This apparently causes problems for the admin app, although it works fine in the view/template etc...

edit... however, only when I removed the ordering field in the Meta class completely from both inheriting class and base class did it work. Eventually I reinstalled django using the latest version, 3.2.9, and everything worked fine. I forced a reinstall --no-cache-dir pip of the version I had before, django-3.2.0 and it failed again.

It seems that the ordering clause doesn't work with the django-3.2.0, so I reinstalled using the latest django and it is now working fine. However, I made a sample app, and that works fine with the ordering clause and django-3.2, so I am not sure what might be wrong in my base code, but it works with django-3.2.9.

my sample app:
https://github.com/millerthegorilla/django_sample_app

my main app:
https://github.com/millerthegorilla/django_artisan (settings live in a separate repository - https://github.com/millerthegorilla/artisan_scripts)

Last edited 3 years ago by James Miller (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top