From b81e974e9ea16bd693b194a728f77fb825ec8e54 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 29 May 2023 21:59:22 -0700 Subject: Fixed #34604 -- Corrected fallback SQL for n-ary logical XOR. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An n-ary logical XOR Q(…) ^ Q(…) ^ … ^ Q(…) should evaluate to true when an odd number of its operands evaluate to true, not when exactly one operand evaluates to true. --- docs/ref/models/querysets.txt | 15 ++++++++++++--- docs/releases/5.0.txt | 5 +++++ 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'docs') diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 1d684607f1..640818d2a5 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -2021,7 +2021,8 @@ may be generated. XOR (``^``) ~~~~~~~~~~~ -Combines two ``QuerySet``\s using the SQL ``XOR`` operator. +Combines two ``QuerySet``\s using the SQL ``XOR`` operator. A ``XOR`` +expression matches rows that are matched by an odd number of operands. The following are equivalent:: @@ -2044,13 +2045,21 @@ SQL equivalent: .. code-block:: sql (x OR y OR ... OR z) AND - 1=( + 1=MOD( (CASE WHEN x THEN 1 ELSE 0 END) + (CASE WHEN y THEN 1 ELSE 0 END) + ... - (CASE WHEN z THEN 1 ELSE 0 END) + + (CASE WHEN z THEN 1 ELSE 0 END), + 2 ) + .. versionchanged:: 5.0 + + In older versions, on databases without native support for the SQL + ``XOR`` operator, ``XOR`` returned rows that were matched by exactly + one operand. The previous behavior was not consistent with MySQL, + MariaDB, and Python behavior. + Methods that do not return ``QuerySet``\s ----------------------------------------- diff --git a/docs/releases/5.0.txt b/docs/releases/5.0.txt index 98bd5d2a9f..fb446fcd7a 100644 --- a/docs/releases/5.0.txt +++ b/docs/releases/5.0.txt @@ -424,6 +424,11 @@ Miscellaneous a page. Having two ``

`` elements was confusing and the site header wasn't helpful as it is repeated on all pages. +* On databases without native support for the SQL ``XOR`` operator, ``^`` as + the exclusive or (``XOR``) operator now returns rows that are matched by an + odd number of operands rather than exactly one operand. This is consistent + with the behavior of MySQL, MariaDB, and Python. + .. _deprecated-features-5.0: Features deprecated in 5.0 -- cgit v1.3