summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2023-02-23 21:12:17 +0100
committerGitHub <noreply@github.com>2023-02-23 21:12:17 +0100
commit16c966ff7fc9ce01e3afd87ef2af55859cadb587 (patch)
tree8701aeb960ddfccc70e6223cf2bb6c234eb55c2a
parent5b3d3e400ab9334ba429ca360c9818c6dfc3a51b (diff)
Refs #30060, Refs #34217 -- Made SchemaEditor not generate SQL for CheckConstraint if not supported.
The new logic mirrors the logic in SchemaEditor._delete_check_sql() added in 68ef274bc505cd44f305c03cbf84cf08826200a8. Thanks Tim Graham for the report.
-rw-r--r--django/db/backends/base/schema.py2
-rw-r--r--tests/migrations/test_operations.py27
2 files changed, 24 insertions, 5 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
index 070211f6d2..db7e4c69c4 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -1726,6 +1726,8 @@ class BaseDatabaseSchemaEditor:
}
def _create_check_sql(self, model, name, check):
+ if not self.connection.features.supports_table_check_constraints:
+ return None
return Statement(
self.sql_create_check,
table=Table(model._meta.db_table, self.quote_name),
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 2f97659046..df12ff428d 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -3555,7 +3555,6 @@ class OperationTests(OperationTestBase):
self.assertIndexNotExists(table_name, ["pink", "weight"])
self.assertUniqueConstraintExists(table_name, ["pink", "weight"])
- @skipUnlessDBFeature("supports_table_check_constraints")
def test_add_constraint(self):
project_state = self.set_up_test_model("test_addconstraint")
gt_check = models.Q(pink__gt=2)
@@ -3581,11 +3580,20 @@ class OperationTests(OperationTestBase):
Pony = new_state.apps.get_model("test_addconstraint", "Pony")
self.assertEqual(len(Pony._meta.constraints), 1)
# Test the database alteration
- with connection.schema_editor() as editor:
+ with (
+ CaptureQueriesContext(connection) as ctx,
+ connection.schema_editor() as editor,
+ ):
gt_operation.database_forwards(
"test_addconstraint", editor, project_state, new_state
)
- with self.assertRaises(IntegrityError), transaction.atomic():
+ if connection.features.supports_table_check_constraints:
+ with self.assertRaises(IntegrityError), transaction.atomic():
+ Pony.objects.create(pink=1, weight=1.0)
+ else:
+ self.assertIs(
+ any("CHECK" in query["sql"] for query in ctx.captured_queries), False
+ )
Pony.objects.create(pink=1, weight=1.0)
# Add another one.
lt_check = models.Q(pink__lt=100)
@@ -3600,11 +3608,20 @@ class OperationTests(OperationTestBase):
)
Pony = new_state.apps.get_model("test_addconstraint", "Pony")
self.assertEqual(len(Pony._meta.constraints), 2)
- with connection.schema_editor() as editor:
+ with (
+ CaptureQueriesContext(connection) as ctx,
+ connection.schema_editor() as editor,
+ ):
lt_operation.database_forwards(
"test_addconstraint", editor, project_state, new_state
)
- with self.assertRaises(IntegrityError), transaction.atomic():
+ if connection.features.supports_table_check_constraints:
+ with self.assertRaises(IntegrityError), transaction.atomic():
+ Pony.objects.create(pink=100, weight=1.0)
+ else:
+ self.assertIs(
+ any("CHECK" in query["sql"] for query in ctx.captured_queries), False
+ )
Pony.objects.create(pink=100, weight=1.0)
# Test reversal
with connection.schema_editor() as editor: