当前位置: 首页>>代码示例>>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;未经允许,请勿转载。