Refs -- Added tests for combined expressions in CheckConstraint.check.
Thanks Adam Johnson for the report.
Fixed in
@@ -1816,6 +1816,34 @@ class OperationTests(OperationTestBase):
Pony(pink=3, weight=3.0),
])
+ @skipUnlessDBFeature('supports_table_check_constraints')
+ def test_add_constraint_combinable(self):
+ app_label = 'test_addconstraint_combinable'
+ operations = [
+ CreateModel(
+ 'Book',
+ fields=[
+ ('id', models.AutoField(primary_key=True)),
+ ('read', models.PositiveIntegerField()),
+ ('unread', models.PositiveIntegerField()),
+ ],
+ ),
+ ]
+ from_state = self.apply_operations(app_label, ProjectState(), operations)
+ constraint = models.CheckConstraint(
+ check=models.Q(read=(100 - models.F('unread'))),
+ name='test_addconstraint_combinable_sum_100',
+ )
+ operation = migrations.AddConstraint('Book', 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)
+ Book = to_state.apps.get_model(app_label, 'Book')
+ with self.assertRaises(IntegrityError), transaction.atomic():
+ 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", constraints=[
