Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#24079 closed Bug (fixed)

Duplicate results of `ManyToManyField` when using 'through'

Reported by: Jean-Christophe Owned by: nobody
Component: Database layer (models, ORM) Version: 1.7
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 Jean-Christophe)

When using a many 2 many relation ship with 'through' parameter, the DB is returning duplicates when several relations exists for the same pair of objects.

I have the following relations :

              tag 1
      user 1 ----->  article 1
              tag 2
      user 1 ----->  article 1

Expected behavior :

user = User.objects.get(pk=1)
print user.article.all()
[<Article: 1>]

Observed behavior

user = User.objects.get(pk=1)
print user.article.all()
[<Article: 1>, <Article: 1>]

More details on Stack Overflow : http://stackoverflow.com/questions/27777065/duplicate-results-of-manytomanyfield-when-using-though

Change History (2)

comment:1 by Jean-Christophe, 9 years ago

Resolution: fixed
Status: newclosed

Solved (http://stackoverflow.com/a/27777286/2550237)
One should use distinct() to remove duplicate entries.

I think a comment in the doc should be appropriated in the many 2 many section.

comment:2 by Jean-Christophe, 9 years ago

Description: modified (diff)
Summary: Duplicate results of `ManyToManyField` when using 'though'Duplicate results of `ManyToManyField` when using 'through'
Note: See TracTickets for help on using tickets.
Back to Top