mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +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:
		| @@ -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), | ||||
|   | ||||
| @@ -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: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user