當前位置: 首頁>>代碼示例>>Python>>正文


Python migrations.AddConstraint方法代碼示例

本文整理匯總了Python中django.db.migrations.AddConstraint方法的典型用法代碼示例。如果您正苦於以下問題:Python migrations.AddConstraint方法的具體用法?Python migrations.AddConstraint怎麽用?Python migrations.AddConstraint使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在django.db.migrations的用法示例。


在下文中一共展示了migrations.AddConstraint方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_add_constraint

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import AddConstraint [as 別名]
def test_add_constraint(self):
        project_state = self.set_up_test_model("test_addconstraint")
        gt_check = models.Q(pink__gt=2)
        gt_constraint = models.CheckConstraint(check=gt_check, name="test_add_constraint_pony_pink_gt_2")
        gt_operation = migrations.AddConstraint("Pony", gt_constraint)
        self.assertEqual(
            gt_operation.describe(), "Create constraint test_add_constraint_pony_pink_gt_2 on model Pony"
        )
        # Test the state alteration
        new_state = project_state.clone()
        gt_operation.state_forwards("test_addconstraint", new_state)
        self.assertEqual(len(new_state.models["test_addconstraint", "pony"].options["constraints"]), 1)
        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:
            gt_operation.database_forwards("test_addconstraint", editor, project_state, new_state)
        with self.assertRaises(IntegrityError), transaction.atomic():
            Pony.objects.create(pink=1, weight=1.0)
        # Add another one.
        lt_check = models.Q(pink__lt=100)
        lt_constraint = models.CheckConstraint(check=lt_check, name="test_add_constraint_pony_pink_lt_100")
        lt_operation = migrations.AddConstraint("Pony", lt_constraint)
        lt_operation.state_forwards("test_addconstraint", new_state)
        self.assertEqual(len(new_state.models["test_addconstraint", "pony"].options["constraints"]), 2)
        Pony = new_state.apps.get_model("test_addconstraint", "Pony")
        self.assertEqual(len(Pony._meta.constraints), 2)
        with connection.schema_editor() as editor:
            lt_operation.database_forwards("test_addconstraint", editor, project_state, new_state)
        with self.assertRaises(IntegrityError), transaction.atomic():
            Pony.objects.create(pink=100, weight=1.0)
        # Test reversal
        with connection.schema_editor() as editor:
            gt_operation.database_backwards("test_addconstraint", editor, new_state, project_state)
        Pony.objects.create(pink=1, weight=1.0)
        # Test deconstruction
        definition = gt_operation.deconstruct()
        self.assertEqual(definition[0], "AddConstraint")
        self.assertEqual(definition[1], [])
        self.assertEqual(definition[2], {'model_name': "Pony", 'constraint': gt_constraint}) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:42,代碼來源:test_operations.py

示例2: test_add_partial_unique_constraint

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import AddConstraint [as 別名]
def test_add_partial_unique_constraint(self):
        project_state = self.set_up_test_model('test_addpartialuniqueconstraint')
        partial_unique_constraint = models.UniqueConstraint(
            fields=['pink'],
            condition=models.Q(weight__gt=5),
            name='test_constraint_pony_pink_for_weight_gt_5_uniq',
        )
        operation = migrations.AddConstraint('Pony', partial_unique_constraint)
        self.assertEqual(
            operation.describe(),
            'Create constraint test_constraint_pony_pink_for_weight_gt_5_uniq '
            'on model Pony'
        )
        # Test the state alteration
        new_state = project_state.clone()
        operation.state_forwards('test_addpartialuniqueconstraint', new_state)
        self.assertEqual(len(new_state.models['test_addpartialuniqueconstraint', 'pony'].options['constraints']), 1)
        Pony = new_state.apps.get_model('test_addpartialuniqueconstraint', 'Pony')
        self.assertEqual(len(Pony._meta.constraints), 1)
        # Test the database alteration
        with connection.schema_editor() as editor:
            operation.database_forwards('test_addpartialuniqueconstraint', editor, project_state, new_state)
        # Test constraint works
        Pony.objects.create(pink=1, weight=4.0)
        Pony.objects.create(pink=1, weight=4.0)
        Pony.objects.create(pink=1, weight=6.0)
        if connection.features.supports_partial_indexes:
            with self.assertRaises(IntegrityError), transaction.atomic():
                Pony.objects.create(pink=1, weight=7.0)
        else:
            Pony.objects.create(pink=1, weight=7.0)
        # Test reversal
        with connection.schema_editor() as editor:
            operation.database_backwards('test_addpartialuniqueconstraint', editor, new_state, project_state)
        # Test constraint doesn't work
        Pony.objects.create(pink=1, weight=7.0)
        # Test deconstruction
        definition = operation.deconstruct()
        self.assertEqual(definition[0], 'AddConstraint')
        self.assertEqual(definition[1], [])
        self.assertEqual(definition[2], {'model_name': 'Pony', 'constraint': partial_unique_constraint}) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:43,代碼來源:test_operations.py


注:本文中的django.db.migrations.AddConstraint方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。