Code

Opened 8 years ago

Closed 6 years ago

#1050 closed defect (fixed)

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

Reported by: ph@… 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: UI/UX:

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)

hacked_Q_for_isnull.py (1.0 KB) - added by Ben Slavin <benjamin.slavin@…> 7 years ago.
Monkey patch as a temporary work-around

Download all attachments as: .zip

Change History (8)

comment:1 Changed 8 years ago by ph@…

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

comment:2 Changed 7 years ago by bo@…

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 Changed 7 years ago by Gary Wilson <gary.wilson@…>

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 7 years ago by Ben Slavin <benjamin.slavin@…>

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)

Changed 7 years ago by Ben Slavin <benjamin.slavin@…>

Monkey patch as a temporary work-around

comment:5 Changed 7 years ago by mtredinnick

  • Keywords orm, qs-rf added; orm removed

comment:6 Changed 7 years ago by mtredinnick

  • Keywords qs-rf-fixed added; qs-rf removed

comment:7 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to 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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.