Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27727 closed Bug (duplicate)

Exclude with many condition in the same related field's fields

Reported by: Mounir Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When trying to exclude with many conditions in the same related field the generated query (tested in Postgresql) will not be the desired one.
E.g. If I have a model Student and a model Teacher, Student have a ForeignKey to Teacher model.

Teacher.objects.exclude(student__age=20, student__note__lt=5).query

Which will results in this query:

SELECT "teachers_teacher"."id", "teachers_teacher"."name" FROM "teachers_teacher" WHERE NOT ("teachers_teacher"."id" IN (SELECT U1."teacher_id" AS Col1 FROM "teachers_student" U1 WHERE U1."age" = 20) AND "teachers_teacher"."id" IN (SELECT U1."teacher_id" AS Col1 FROM "teachers_student" U1 WHERE U1."note" < 5))

I think the right SQL will be the following:

SELECT "teachers_teacher"."id", "teachers_teacher"."name" FROM "teachers_teacher" WHERE NOT ("teachers_teacher"."id" IN (SELECT U1."teacher_id" AS Col1 FROM "teachers_student" U1 WHERE U1."age" = 20 AND U1."note" < 5))

Even if the fix will be need a lot of work, I think it's better to mention this in the exclude documentation.

Change History (2)

comment:1 by Tim Graham, 8 years ago

Resolution: duplicate
Status: newclosed

I believe that's a duplicate of #14645. The documentation already contains a note about it.

in reply to:  1 comment:2 by Mounir, 8 years ago

Thank you and sorry for the duplicate, I would suggest to mention this in the documentation of the exclude

Replying to Tim Graham:

I believe that's a duplicate of #14645. The documentation already contains a note about it.

Note: See TracTickets for help on using tickets.
Back to Top