Django

Code

Ticket #1050 (closed: fixed)

Opened 2 years ago

Last modified 3 weeks ago

Can't find null objects in a ManyToMany that may be null

Reported by: ph@malaprop.org Assigned to: nobody
Component: Database wrapper Version:
Keywords: null, manytomany, orm, qs-rf-fixed Cc:
Triage Stage: Accepted Has patch: 0
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

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

hacked_Q_for_isnull.py (1.0 kB) - added by Ben Slavin <benjamin.slavin@gmail.com> on 07/01/07 16:38:37.
Monkey patch as a temporary work-around

Change History

12/12/05 13:42:17 changed by ph@malaprop.org

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

01/13/07 15:48:01 changed by bo@theaddedones.com

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

02/26/07 16:25:07 changed by Gary Wilson <gary.wilson@gmail.com>

  • stage changed from Unreviewed to Accepted.

07/01/07 16:38:13 changed by Ben Slavin <benjamin.slavin@gmail.com>

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)

07/01/07 16:38:37 changed by Ben Slavin <benjamin.slavin@gmail.com>

  • attachment hacked_Q_for_isnull.py added.

Monkey patch as a temporary work-around

09/13/07 16:16:13 changed by mtredinnick

  • keywords changed from null, manytomany, orm to null, manytomany, orm, qs-rf.

10/13/07 21:20:17 changed by mtredinnick

  • keywords changed from null, manytomany, orm, qs-rf to null, manytomany, orm, qs-rf-fixed.

04/26/08 21:50:16 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(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


Add/Change #1050 (Can't find null objects in a ManyToMany that may be null)




Change Properties
Action