#15807: order by 'pk' alias doesn't work with inherited models when parent has an ordering set -------------------------------------+------------------------------------- Reporter: mat | Owner: nobody Type: Bug | Status: closed Milestone: | Component: Database layer Version: SVN | (models, ORM) Resolution: wontfix | Severity: Normal Triage Stage: | Keywords: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 -------------------------------------+------------------------------------- Changes (by jacob):
* status: new => closed * resolution: => wontfix * easy: => 0 Comment: So here's why this is happening: `pk` is translated to "the name of the primary key column". `Child`, as a concrete subclass, has a primary key that has a foreign key (technically a `OneToOneField` with `parent_link=True`) to `Foo`. In other words, your models, with the field implicitly added by the subclassing, ''actually'' are like: {{{ #!python class Foo(models.Model): bar = models.PositiveSmallIntegerField(default=1) class Child(models.Model): foo_ptr = models.OneToOneField(Foo, parent_link=True) barchild = models.PositiveSmallIntegerField(default=1) }}} This means that `Child.objects.order_by("pk")` actually translates to `Child.objects.order_by("foo_ptr")`. Remember, `foo_ptr` is a foreign key (one-to-one fields are just a special kind of foreign key). This is important, because `order_by("some_foreign_key")` translates to "order by the default ordering for the object pointed to by the foreign key". As [http://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by the documentation says]: If you try to order by a field that is a relation to another model, Django will use the default ordering on the related model (or order by the related model's primary key if there is no Meta.ordering specified. The object pointed to, in this case, is `Foo`. So `Child.objects.order_by("pk")` is in fact working correctly, if a bit counterintuitively. We ''could'' change this, but it would introduce other subtle counterintuitive problems and would quite likely be backwards- incompatible. So I'm going to mark this wontfix. I know it's not ideal, but I'm afraid the cure is worse than the disease here. -- Ticket URL: <http://code.djangoproject.com/ticket/15807#comment:2> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.