From 68ef274bc505cd44f305c03cbf84cf08826200a8 Mon Sep 17 00:00:00 2001 From: DevilsAutumn Date: Sun, 25 Dec 2022 21:33:02 +0530 Subject: [PATCH] Fixed #34217 -- Fixed migration crash when removing check constraints on MySQL < 8.0.16. --- django/db/backends/base/schema.py | 2 ++ tests/migrations/test_operations.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 6697848191..2451fc515a 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -1651,6 +1651,8 @@ class BaseDatabaseSchemaEditor: ) def _delete_check_sql(self, model, name): + if not self.connection.features.supports_table_check_constraints: + return None return self._delete_constraint_sql(self.sql_delete_check, model, name) def _delete_constraint_sql(self, template, model, name): diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index dc0d34eebd..2bcb38feae 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -3630,7 +3630,6 @@ class OperationTests(OperationTestBase): Book.objects.create(read=70, unread=10) Book.objects.create(read=70, unread=30) - @skipUnlessDBFeature("supports_table_check_constraints") def test_remove_constraint(self): project_state = self.set_up_test_model( "test_removeconstraint", @@ -3673,7 +3672,10 @@ class OperationTests(OperationTestBase): "test_removeconstraint", editor, project_state, new_state ) Pony.objects.create(pink=1, weight=1.0).delete() - 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: Pony.objects.create(pink=100, weight=1.0) # Remove the other one. lt_operation = migrations.RemoveConstraint( @@ -3698,7 +3700,10 @@ class OperationTests(OperationTestBase): gt_operation.database_backwards( "test_removeconstraint", editor, new_state, project_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: Pony.objects.create(pink=1, weight=1.0) # Test deconstruction definition = gt_operation.deconstruct()