|
@@ -1898,6 +1898,29 @@ class OperationTests(OperationTestBase):
|
|
|
author = Author.objects.create(name='Albert', rebate='10%')
|
|
|
self.assertEqual(Author.objects.get(), author)
|
|
|
|
|
|
+ @skipUnlessDBFeature('supports_table_check_constraints')
|
|
|
+ def test_add_or_constraint(self):
|
|
|
+ app_label = 'test_addorconstraint'
|
|
|
+ constraint_name = 'add_constraint_or'
|
|
|
+ from_state = self.set_up_test_model(app_label)
|
|
|
+ check = models.Q(pink__gt=2, weight__gt=2) | models.Q(weight__lt=0)
|
|
|
+ constraint = models.CheckConstraint(check=check, name=constraint_name)
|
|
|
+ operation = migrations.AddConstraint('Pony', constraint)
|
|
|
+ to_state = from_state.clone()
|
|
|
+ operation.state_forwards(app_label, to_state)
|
|
|
+ with connection.schema_editor() as editor:
|
|
|
+ operation.database_forwards(app_label, editor, from_state, to_state)
|
|
|
+ Pony = to_state.apps.get_model(app_label, 'Pony')
|
|
|
+ with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
+ Pony.objects.create(pink=2, weight=3.0)
|
|
|
+ with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
+ Pony.objects.create(pink=3, weight=1.0)
|
|
|
+ Pony.objects.bulk_create([
|
|
|
+ Pony(pink=3, weight=-1.0),
|
|
|
+ Pony(pink=1, weight=-1.0),
|
|
|
+ Pony(pink=3, weight=3.0),
|
|
|
+ ])
|
|
|
+
|
|
|
@skipUnlessDBFeature('supports_table_check_constraints')
|
|
|
def test_remove_constraint(self):
|
|
|
project_state = self.set_up_test_model("test_removeconstraint", constraints=[
|