Opened 18 years ago
Closed 17 years ago
#2091 closed defect (fixed)
IN field lookup returns all objects without filtering by the supplied list when used on ManyToMany field.
Reported by: | real dot human at mrmachine dot net | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | normal | Keywords: | in field lookup filter manytomany m2m many, qs-rf-fixed |
Cc: | gabor@…, freakboy3742@…, real.human@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
using an IN field lookup in a filter on a ManyToMany field seems to return all objects. as far as i know, the two e.filter statements below should return the same queryset.
>>> e = Entry.objects.all() >>> e [<Entry: smooth sailing>, <Entry: wii rocks e3>, <Entry: 30thirty reboot>] >>> t = Tag.objects.get(id=6) >>> t <Tag: django> >>> t.entry_set.all() [<Entry: smooth sailing>, <Entry: 30thirty reboot>] >>> e.filter(tags__id__exact=6) [<Entry: smooth sailing>, <Entry: 30thirty reboot>] >>> e.filter(tags__id__in=[6]) [<Entry: smooth sailing>, <Entry: wii rocks e3>, <Entry: 30thirty reboot>]
Change History (9)
comment:1 by , 18 years ago
comment:2 by , 18 years ago
Cc: | added |
---|
i had the same problem, but for me it's only happening with sqlite3.
which database did you use?
and if you used sqlite3, could you try to reproduce the problem with postgresql?
comment:3 by , 18 years ago
Cc: | added |
---|
I see this problem as well, but only with SQLite3. It doesn't appear to be a problem with PostgreSQL.
The problem appears to be caused by the fact that m2m tables are collated using a LEFT OUTER JOIN, rather than an INNER JOIN. Using SQLite, this approach puts partially sparse rows into the combined table for those m2m entries that should be excluded from the result set. PostgreSQL omits the sparse rows, and therefore returns the correct results.
If you hand modify the query produced by the filter operation and replace the outer join with an inner join, the results return as expected.
The use of LEFT OUTER JOIN was introduced in r2613 to fix #1535; however, it has caused other problems, such as #2306.
comment:4 by , 18 years ago
Owner: | changed from | to
---|
comment:5 by , 18 years ago
this issue seems to be very similar to the one in #2457. maybe it's the same issue?
comment:6 by , 18 years ago
Cc: | added |
---|
comment:7 by , 18 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:8 by , 17 years ago
Keywords: | qs-rf-fixed added |
---|
comment:9 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
(In [7477]) Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
here's some additional information showing the broken sql being generated.