diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index ac8222b7b0..0575f91a9f 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -626,6 +626,11 @@ class Query(object): alias, False) alias = joins[-1] col = target.column + if not field.rel: + # To avoid inadvertent trimming of a necessary alias, use the + # refcount to show that we are referencing a non-relation field on + # the model. + self.ref_alias(alias) # Must use left outer joins for nullable fields. for join in joins: diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index d7938f63a9..e1fff9e874 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -172,6 +172,17 @@ class Child(models.Model): person = models.OneToOneField(Member, primary_key=True) parent = models.ForeignKey(Member, related_name="children") +# Custom primary keys interfered with ordering in the past. +class CustomPk(models.Model): + name = models.CharField(max_length=10, primary_key=True) + extra = models.CharField(max_length=10) + + class Meta: + ordering = ['name', 'extra'] + +class Related(models.Model): + custom = models.ForeignKey(CustomPk) + __test__ = {'API_TESTS':""" >>> t1 = Tag.objects.create(name='t1') @@ -786,5 +797,9 @@ Bug #7277 >>> n1.annotation_set.filter(Q(tag=t5) | Q(tag__children=t5) | Q(tag__children__children=t5)) [] +Bug #7371 +>>> Related.objects.order_by('custom') +[] + """}