﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18165	Ordering by related field creates duplicates in resultant querysets	Mathijs de Bruin	nobody	"When specifying an `ordering` property in a Model's Meta class, based on a field in a model with a ForeignKey relation to this model, duplicate objects are returned in the resulting queryset.

A test application with failing unittest is attached to the report in a tarball. In summary, the following models and query code can be used to replicate this issue:

{{{
    class TestModel(models.Model):
        class Meta:
            ordering = ('testrelated__field', )


    class TestRelated(models.Model):
        field = models.IntegerField()

        testmodel = models.ForeignKey(TestModel)
}}}

Now the following behaviour can be observed:

{{{
    >>> o = TestModel()
    >>> o.save()
    >>> r1 = TestRelated(field=1, testmodel=o)
    >>> r2 = TestRelated(field=2, testmodel=o)
    >>> r1.save()
    >>> r2.save()
    >>> TestModel.objects.all()
    [<TestModel: 1>, <TestModel: 1>]
}}}

The behaviour has found to exist for the SQLite backend and might or might not occur with other database backends."	Bug	closed	Database layer (models, ORM)	1.4	Normal	wontfix	ordering, duplicates, related		Unreviewed	0	0	0	0	0	0
