From 12245eb7bb9b402a92354d4534c65b91cde03663 Mon Sep 17 00:00:00 2001 From: Casey Brooks Date: Wed, 24 Dec 2025 16:50:47 +0000 Subject: [PATCH 1/2] test(lookups): cover unregister lookup cache --- tests/custom_lookups/tests.py | 2 ++ tests/schema/tests.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/tests/custom_lookups/tests.py b/tests/custom_lookups/tests.py index 1cf99b8300da..943c99f18f76 100644 --- a/tests/custom_lookups/tests.py +++ b/tests/custom_lookups/tests.py @@ -324,6 +324,8 @@ def test_lookups_caching(self): # getting the lookups again should re-cache self.assertIn("exactly", field.get_lookups()) + self.assertNotIn("exactly", field.get_lookups()) + class BilateralTransformTests(TestCase): def test_bilateral_upper(self): diff --git a/tests/schema/tests.py b/tests/schema/tests.py index fa59a3e0b137..e2bc98a3e265 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -2770,6 +2770,10 @@ def test_func_unique_constraint_lookups(self): with connection.schema_editor() as editor: editor.add_constraint(Author, constraint) sql = constraint.create_sql(Author, editor) + name_field = Author._meta.get_field("name") + weight_field = Author._meta.get_field("weight") + self.assertIsNone(name_field.get_transform("lower")) + self.assertIsNone(weight_field.get_transform("abs")) table = Author._meta.db_table constraints = self.get_constraints(table) self.assertIn(constraint.name, constraints) From 2612034de6503682e809a19dc0cfb80b0482dd73 Mon Sep 17 00:00:00 2001 From: Casey Brooks Date: Wed, 24 Dec 2025 16:56:55 +0000 Subject: [PATCH 2/2] fix(lookups): clear cache on unregister --- django/db/models/query_utils.py | 1 + tests/schema/tests.py | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 0caa165e151b..6917820604b4 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -217,6 +217,7 @@ def _unregister_lookup(cls, lookup, lookup_name=None): if lookup_name is None: lookup_name = lookup.lookup_name del cls.class_lookups[lookup_name] + cls._clear_cached_lookups() def select_related_descend(field, restricted, requested, load_fields, reverse=False): diff --git a/tests/schema/tests.py b/tests/schema/tests.py index e2bc98a3e265..68e008cd5bb6 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -2770,20 +2770,20 @@ def test_func_unique_constraint_lookups(self): with connection.schema_editor() as editor: editor.add_constraint(Author, constraint) sql = constraint.create_sql(Author, editor) + table = Author._meta.db_table + constraints = self.get_constraints(table) + self.assertIn(constraint.name, constraints) + self.assertIs(constraints[constraint.name]["unique"], True) + # SQL contains columns. + self.assertIs(sql.references_column(table, "name"), True) + self.assertIs(sql.references_column(table, "weight"), True) + # Remove constraint. + with connection.schema_editor() as editor: + editor.remove_constraint(Author, constraint) name_field = Author._meta.get_field("name") weight_field = Author._meta.get_field("weight") self.assertIsNone(name_field.get_transform("lower")) self.assertIsNone(weight_field.get_transform("abs")) - table = Author._meta.db_table - constraints = self.get_constraints(table) - self.assertIn(constraint.name, constraints) - self.assertIs(constraints[constraint.name]["unique"], True) - # SQL contains columns. - self.assertIs(sql.references_column(table, "name"), True) - self.assertIs(sql.references_column(table, "weight"), True) - # Remove constraint. - with connection.schema_editor() as editor: - editor.remove_constraint(Author, constraint) self.assertNotIn(constraint.name, self.get_constraints(table)) @skipUnlessDBFeature("supports_expression_indexes")