Skip to content

Conversation

@Eddy-123
Copy link

@Eddy-123 Eddy-123 commented Oct 31, 2025

Trac ticket number

ticket-20024

Branch description

Problem
The SQL generated by an exclude queryset is not semantically aligned with the queryset when using __in lookups containing None.

>>> str(Entry.objects.exclude(foo__in=[None, 1]).query)
'SELECT "core_entry"."id", "core_entry"."foo" FROM "core_entry" WHERE NOT ("core_entry"."foo" IN (1) AND "core_entry"."foo" IS NOT NULL)'

Solution
The PR updates WhereNode to use OR and generate IS NULL for correct SQL semantics in this case.

>>> str(Entry.objects.exclude(foo__in=[None, 1]).query)
'SELECT "core_entry"."id", "core_entry"."foo" FROM "core_entry" WHERE NOT (("core_entry"."foo" IN (1) OR "core_entry"."foo" IS NULL))'

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

… exclude querysets to match SQL semantics.
Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@charettes reviewed #19691, so he might have some thoughts.

# into subquery above
self.assertIs(inner_qs._result_cache, None)

@unittest.expectedFailure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were some tests included with #19691 we should add.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The composite pk one seems valuable, not sure about the other one though 🤔

clause.add(lookup_class(col, False), AND)
if (
lookup_type == "in"
and isinstance(condition.rhs, (list, tuple))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should accept any iterable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that we have to hardcode this logic here but hopefully #16817 would allow us to revisit this properly by having lookups.In.exclude_nulls return the proper constraint.

Copy link
Member

@charettes charettes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the right approach to me!

clause.add(lookup_class(col, False), AND)
if (
lookup_type == "in"
and isinstance(condition.rhs, (list, tuple))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that we have to hardcode this logic here but hopefully #16817 would allow us to revisit this properly by having lookups.In.exclude_nulls return the proper constraint.

# into subquery above
self.assertIs(inner_qs._result_cache, None)

@unittest.expectedFailure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The composite pk one seems valuable, not sure about the other one though 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants