Opened 19 years ago
Closed 17 years ago
#1050 closed defect (fixed)
Can't find null objects in a ManyToMany that may be null
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | |
Severity: | normal | Keywords: | null, manytomany, orm, qs-rf-fixed |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Model B defined a ManyToMany relationship to Model A that can be null. There's no way in the ORM to look up all the Bs where it's null. There might be a workaround with a custom SQL query.
Jacobkm in IRC told me to post a ticket so he doesn't forget to fix this.
Attachments (1)
Change History (8)
comment:1 by , 19 years ago
comment:2 by , 18 years ago
I think the 'easiest' way to allow for this case is to allow one to change the JOIN type .. currently, using INNER JOINS is the issue .. a LEFT JOIN would allow you to do this
MyModel.objects.filter(manytomany__id__isnull = True)
however, there is no method to 'detect' the isnull on the primary key ... i.e. change this
SELECT ... FROM app_mymodel INNER JOIN app_manytomany AS app_app_manytomany ON app_mymodel.id = app_app_manytomany.asset_id WHERE (app_app_manytomany.id IS NULL)
to
SELECT ... FROM app_mymodel LEFT JOIN app_manytomany AS app_app_manytomany ON app_mymodel.id = app_app_manytomany.mymodel_id WHERE (app_app_manytomany.id IS NULL)
the former will always return nothing (unless of course the id can be null, but since when should a primary key autoinc field be null? :)
so maybe it can detect something like
MyModel.objects.filter(manytomany__pk__isnull = True)
(the pk flag to force this this to be true) ..
the issue with not supporting this, means the usual filter chaining cannot be done as it currently needs be a special static query
comment:3 by , 18 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 17 years ago
Howdy folks.
I just ran into this and came-up with a monkey patch around the Q object to enable this. It's useful for me, so I figured I'd post it here.
This is not a long term fix, but should help to hold people over until the QuerySet refactor.
Again... this isn't something I intend to be included in trunk (and recommend against it)
comment:5 by , 17 years ago
Keywords: | qs-rf added |
---|
comment:6 by , 17 years ago
Keywords: | qs-rf-fixed added; qs-rf removed |
---|
comment:7 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
Yeah, workaround is the SQL: SELECT app_as.id FROM app_as LEFT JOIN app_as_bs ON app_as.id = app_as_bs.a_id WHERE app_as_bs.a_id IS NULL