Opened 7 years ago

Closed 7 years ago

#28149 closed New feature (duplicate)

QuerySet model fields comparison

Reported by: Vladislav Lutkov Owned by: nobody
Component: Database layer (models, ORM) Version:
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

AModel.objects.only('id').filter(b_model__b_field=F('a_field'))

generates

SELECT `a_table`.`id` FROM `a_table` INNER JOIN `b_table` ON (`a_table`.`id` = `b_table`.`a_table_id`) WHERE `b_table`.`b_field` = (`a_table`.`a_field`)

and it's OK, but if you need to make '<>' or '!=' operation between fields you try

AModel.objects.only('id').exclude(b_model__b_field=F('a_field'))

and it generates

SELECT `a_table`.`id` FROM `a_table` WHERE NOT (`a_table`.`id` IN (SELECT U1.`a_table_id` AS Col1 FROM `a_table` U0 INNER JOIN `b_table` U1 ON (U0.`id` = U1.`a_table_id`) WHERE U1.`b_field` = (U0.`a_field`)))

and if you try

.filter(~Q('...'))

you'll get the same sql query

Need to make a 'ne' lookup (or something else), which will generate '<>' or '!=' sql operators

Change History (2)

comment:1 by Vladislav Lutkov, 7 years ago

Summary: QuerySet model fields comparsionQuerySet model fields comparison

comment:2 by Simon Charette, 7 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: duplicate
Status: newclosed

Duplicate of #5763.

The linked ticket provides more context about the issue.

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