Comparison to None on one-to-one relation does not generate proper query

Issue #985 resolved
Former user created an issue

While comparison to None in one-to-many relations works, for one-to-one it generates completely wrong query, as illustrated in filter-o2o.py

Also, != comparison to None in uselist=False case generates somewhat non leading exception, in contrast to case uselist=True.

But, it seems to me that it isn't much work to extend the ne to work as negated eq in comparison to None (in case of uselist=False, at least).

I've attached a simple patch which cures above mentioned - but it should be taken only as example of what I'm talking about, because I haven't dig into the rest of comparators and don't really understand the code.

Comments (6)

  1. Mike Bayer repo owner

    very nice. You're on the right track but I corrected the patch to check specifically for the "ONETOMANY" "direction", since thats actually whats at play here, and added unit tests for all o2m/m2o/o2o combinations of ==None/!=None in 3175686514dc75acfa3ed0be92f31535325c60dd. thanks for the patch / test !

  2. Former user Account Deleted
    • removed status
    • changed status to open

    This patch has broken ==None comparison for the one-to-many relations though the secondary table.

    filter(Folder.parent == None) now results in

    SELECT folder.id AS folder_id, folder.name AS folder_name
    FROM folder, folder_hier
    WHERE folder.id = folder_hier.child AND folder_hier.parent IS NULL ORDER BY folder.oid
    

    instead of (before the patch applied)

    SELECT folder.id AS folder_id, folder.name AS folder_name
    FROM folder
    WHERE NOT (EXISTS (SELECT 1
    FROM folder_hier
    WHERE folder.id = folder_hier.child)) ORDER BY folder.oid
    
  3. Log in to comment