1
0
mirror of https://github.com/django/django.git synced 2025-01-03 15:06:09 +00:00

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 68ef274bc5.

Thanks Tim Graham for the report.
This commit is contained in:
Mariusz Felisiak 2023-02-23 21:12:17 +01:00 committed by GitHub
parent 5b3d3e400a
commit 16c966ff7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View File

@ -1726,6 +1726,8 @@ class BaseDatabaseSchemaEditor:
} }
def _create_check_sql(self, model, name, check): def _create_check_sql(self, model, name, check):
if not self.connection.features.supports_table_check_constraints:
return None
return Statement( return Statement(
self.sql_create_check, self.sql_create_check,
table=Table(model._meta.db_table, self.quote_name), table=Table(model._meta.db_table, self.quote_name),

View File

@ -3555,7 +3555,6 @@ class OperationTests(OperationTestBase):
self.assertIndexNotExists(table_name, ["pink", "weight"]) self.assertIndexNotExists(table_name, ["pink", "weight"])
self.assertUniqueConstraintExists(table_name, ["pink", "weight"]) self.assertUniqueConstraintExists(table_name, ["pink", "weight"])
@skipUnlessDBFeature("supports_table_check_constraints")
def test_add_constraint(self): def test_add_constraint(self):
project_state = self.set_up_test_model("test_addconstraint") project_state = self.set_up_test_model("test_addconstraint")
gt_check = models.Q(pink__gt=2) gt_check = models.Q(pink__gt=2)
@ -3581,11 +3580,20 @@ class OperationTests(OperationTestBase):
Pony = new_state.apps.get_model("test_addconstraint", "Pony") Pony = new_state.apps.get_model("test_addconstraint", "Pony")
self.assertEqual(len(Pony._meta.constraints), 1) self.assertEqual(len(Pony._meta.constraints), 1)
# Test the database alteration # Test the database alteration
with connection.schema_editor() as editor: with (
CaptureQueriesContext(connection) as ctx,
connection.schema_editor() as editor,
):
gt_operation.database_forwards( gt_operation.database_forwards(
"test_addconstraint", editor, project_state, new_state "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) Pony.objects.create(pink=1, weight=1.0)
# Add another one. # Add another one.
lt_check = models.Q(pink__lt=100) lt_check = models.Q(pink__lt=100)
@ -3600,11 +3608,20 @@ class OperationTests(OperationTestBase):
) )
Pony = new_state.apps.get_model("test_addconstraint", "Pony") Pony = new_state.apps.get_model("test_addconstraint", "Pony")
self.assertEqual(len(Pony._meta.constraints), 2) 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( lt_operation.database_forwards(
"test_addconstraint", editor, project_state, new_state "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) Pony.objects.create(pink=100, weight=1.0)
# Test reversal # Test reversal
with connection.schema_editor() as editor: with connection.schema_editor() as editor: