﻿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
28292	ORing on ManyToMany to same model can result in duplicate items in queryset	Till Theato	nobody	"One of my models has two ManyToManyFields to another model:
{{{
#!python
class Bar(models.Model):
    created = models.DateTimeField(auto_now_add=True)

class Foo(models.Model):
    rel1 = models.ManyToManyField('Bar', related_name='foo_rel1')
    rel2 = models.ManyToManyField('Bar', related_name='foo_rel2')
}}}

I have to query all instances of Foo that point to the same Bar via any of the both relationships. However when an instance of Foo had multiple relationships to unrelated Bar instances, I got duplicated instances in the resulting queryset:
{{{
#!python
b1 = Bar.objects.create()
b2 = Bar.objects.create()
b3 = Bar.objects.create()

f = Foo.objects.create()
f.rel1.set([b2,b3])
f.rel2.set([b1])

assert Foo.objects.filter(rel2=b1).count() == 1 # Good

assert Foo.objects.filter(rel1=b1).count() == 0 # Good

qs = Foo.objects.filter(Q(rel1=b1) | Q(rel2=b1))

assert qs.count() == 1 # AssertionError, qs.count() == 2

print(qs.values('rel1', 'rel2'))
# <QuerySet [{'rel1': 2, 'rel2': 1}, {'rel1': 3, 'rel2': 1}]>
}}}

As the last output shows, one entry per relationship in rel1 is returned."	Bug	closed	Database layer (models, ORM)	1.11	Normal	invalid			Unreviewed	0	0	0	0	0	0
