Opened 8 years ago
Closed 8 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 , 8 years ago
Summary: | QuerySet model fields comparsion → QuerySet model fields comparison |
---|
comment:2 by , 8 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Resolution: | → duplicate |
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
Duplicate of #5763.
The linked ticket provides more context about the issue.