本文整理汇总了Python中django.db.models.UniqueConstraint方法的典型用法代码示例。如果您正苦于以下问题:Python models.UniqueConstraint方法的具体用法?Python models.UniqueConstraint怎么用?Python models.UniqueConstraint使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models
的用法示例。
在下文中一共展示了models.UniqueConstraint方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_eq_with_condition
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_eq_with_condition(self):
self.assertEqual(
models.UniqueConstraint(
fields=['foo', 'bar'], name='unique',
condition=models.Q(foo=models.F('bar'))
),
models.UniqueConstraint(
fields=['foo', 'bar'], name='unique',
condition=models.Q(foo=models.F('bar'))),
)
self.assertNotEqual(
models.UniqueConstraint(
fields=['foo', 'bar'],
name='unique',
condition=models.Q(foo=models.F('bar'))
),
models.UniqueConstraint(
fields=['foo', 'bar'],
name='unique',
condition=models.Q(foo=models.F('baz'))
),
)
示例2: test_add_meta_conditional_multicolumn_unique_constraint__ok
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_add_meta_conditional_multicolumn_unique_constraint__ok():
with cmp_schema_editor() as editor:
editor.add_constraint(Model, models.UniqueConstraint(
fields=('field1', 'field2'), name='field1_field2_uniq', condition=models.Q(field1=models.F('field2'))))
if django.VERSION[:2] >= (3, 0):
assert editor.collected_sql == [
'CREATE UNIQUE INDEX CONCURRENTLY "field1_field2_uniq" ON "tests_model" ("field1", "field2") '
'WHERE "field1" = "field2";',
]
assert editor.django_sql == [
'CREATE UNIQUE INDEX "field1_field2_uniq" ON "tests_model" ("field1", "field2") '
'WHERE "field1" = "field2";',
]
else:
assert editor.collected_sql == [
'CREATE UNIQUE INDEX CONCURRENTLY "field1_field2_uniq" ON "tests_model" ("field1", "field2") '
'WHERE "field1" = ("field2");',
]
assert editor.django_sql == [
'CREATE UNIQUE INDEX "field1_field2_uniq" ON "tests_model" ("field1", "field2") '
'WHERE "field1" = ("field2");',
]
示例3: test_add_partial_unique_constraint
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [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})
示例4: test_eq
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_eq(self):
self.assertEqual(
models.UniqueConstraint(fields=['foo', 'bar'], name='unique'),
models.UniqueConstraint(fields=['foo', 'bar'], name='unique'),
)
self.assertNotEqual(
models.UniqueConstraint(fields=['foo', 'bar'], name='unique'),
models.UniqueConstraint(fields=['foo', 'bar'], name='unique2'),
)
self.assertNotEqual(
models.UniqueConstraint(fields=['foo', 'bar'], name='unique'),
models.UniqueConstraint(fields=['foo', 'baz'], name='unique'),
)
self.assertNotEqual(models.UniqueConstraint(fields=['foo', 'bar'], name='unique'), 1)
示例5: test_repr_with_condition
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_repr_with_condition(self):
constraint = models.UniqueConstraint(
fields=['foo', 'bar'],
name='unique_fields',
condition=models.Q(foo=models.F('bar')),
)
self.assertEqual(
repr(constraint),
"<UniqueConstraint: fields=('foo', 'bar') name='unique_fields' "
"condition=(AND: ('foo', F(bar)))>",
)
示例6: test_deconstruction
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_deconstruction(self):
fields = ['foo', 'bar']
name = 'unique_fields'
constraint = models.UniqueConstraint(fields=fields, name=name)
path, args, kwargs = constraint.deconstruct()
self.assertEqual(path, 'django.db.models.UniqueConstraint')
self.assertEqual(args, ())
self.assertEqual(kwargs, {'fields': tuple(fields), 'name': name})
示例7: test_deconstruction_with_condition
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_deconstruction_with_condition(self):
fields = ['foo', 'bar']
name = 'unique_fields'
condition = models.Q(foo=models.F('bar'))
constraint = models.UniqueConstraint(fields=fields, name=name, condition=condition)
path, args, kwargs = constraint.deconstruct()
self.assertEqual(path, 'django.db.models.UniqueConstraint')
self.assertEqual(args, ())
self.assertEqual(kwargs, {'fields': tuple(fields), 'name': name, 'condition': condition})
示例8: test_condition_must_be_q
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_condition_must_be_q(self):
with self.assertRaisesMessage(ValueError, 'UniqueConstraint.condition must be a Q instance.'):
models.UniqueConstraint(name='uniq', fields=['name'], condition='invalid')
示例9: test_add_meta_unique_constraint__ok
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_add_meta_unique_constraint__ok():
with cmp_schema_editor() as editor:
editor.add_constraint(Model, models.UniqueConstraint(fields=('field1',), name='field1_uniq'))
assert editor.collected_sql == [
'CREATE UNIQUE INDEX CONCURRENTLY "field1_uniq" ON "tests_model" ("field1");',
] + timeouts(
'ALTER TABLE "tests_model" ADD CONSTRAINT "field1_uniq" '
'UNIQUE USING INDEX "field1_uniq";',
)
assert editor.django_sql == [
'ALTER TABLE "tests_model" ADD CONSTRAINT "field1_uniq" UNIQUE ("field1");',
]
示例10: test_add_meta_unique_constraint__with_flexible_timeout__ok
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_add_meta_unique_constraint__with_flexible_timeout__ok():
with cmp_schema_editor() as editor:
editor.add_constraint(Model, models.UniqueConstraint(fields=('field1',), name='field1_uniq'))
assert editor.collected_sql == flexible_statement_timeout(
'CREATE UNIQUE INDEX CONCURRENTLY "field1_uniq" ON "tests_model" ("field1");',
) + timeouts(
'ALTER TABLE "tests_model" ADD CONSTRAINT "field1_uniq" '
'UNIQUE USING INDEX "field1_uniq";',
)
assert editor.django_sql == [
'ALTER TABLE "tests_model" ADD CONSTRAINT "field1_uniq" UNIQUE ("field1");',
]
示例11: test_add_meta_multicolumn_unique_constraint__ok
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_add_meta_multicolumn_unique_constraint__ok():
with cmp_schema_editor() as editor:
editor.add_constraint(Model, models.UniqueConstraint(fields=('field1', 'field2'), name='field1_field2_uniq'))
assert editor.collected_sql == [
'CREATE UNIQUE INDEX CONCURRENTLY "field1_field2_uniq" ON "tests_model" ("field1", "field2");',
] + timeouts(
'ALTER TABLE "tests_model" ADD CONSTRAINT "field1_field2_uniq" '
'UNIQUE USING INDEX "field1_field2_uniq";',
)
assert editor.django_sql == [
'ALTER TABLE "tests_model" ADD CONSTRAINT "field1_field2_uniq" UNIQUE ("field1", "field2");',
]
示例12: test_add_meta_conditional_unique_constraint__ok
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_add_meta_conditional_unique_constraint__ok():
with cmp_schema_editor() as editor:
editor.add_constraint(Model, models.UniqueConstraint(
fields=('field1',), name='field1_uniq', condition=models.Q(field1__gt=0)))
assert editor.collected_sql == [
'CREATE UNIQUE INDEX CONCURRENTLY "field1_uniq" ON "tests_model" ("field1") WHERE "field1" > 0;',
]
assert editor.django_sql == [
'CREATE UNIQUE INDEX "field1_uniq" ON "tests_model" ("field1") WHERE "field1" > 0;',
]
示例13: test_create_model_with_partial_unique_constraint
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_create_model_with_partial_unique_constraint(self):
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.CreateModel(
'Pony',
[
('id', models.AutoField(primary_key=True)),
('pink', models.IntegerField(default=3)),
('weight', models.FloatField()),
],
options={'constraints': [partial_unique_constraint]},
)
# Test the state alteration
project_state = ProjectState()
new_state = project_state.clone()
operation.state_forwards('test_crmo', new_state)
self.assertEqual(len(new_state.models['test_crmo', 'pony'].options['constraints']), 1)
# Test database alteration
self.assertTableNotExists('test_crmo_pony')
with connection.schema_editor() as editor:
operation.database_forwards('test_crmo', editor, project_state, new_state)
self.assertTableExists('test_crmo_pony')
# Test constraint works
Pony = new_state.apps.get_model('test_crmo', 'Pony')
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):
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_crmo', editor, new_state, project_state)
self.assertTableNotExists('test_crmo_pony')
# Test deconstruction
definition = operation.deconstruct()
self.assertEqual(definition[0], 'CreateModel')
self.assertEqual(definition[1], [])
self.assertEqual(definition[2]['options']['constraints'], [partial_unique_constraint])
示例14: test_remove_partial_unique_constraint
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import UniqueConstraint [as 别名]
def test_remove_partial_unique_constraint(self):
project_state = self.set_up_test_model('test_removepartialuniqueconstraint', constraints=[
models.UniqueConstraint(
fields=['pink'],
condition=models.Q(weight__gt=5),
name='test_constraint_pony_pink_for_weight_gt_5_uniq',
),
])
gt_operation = migrations.RemoveConstraint('Pony', 'test_constraint_pony_pink_for_weight_gt_5_uniq')
self.assertEqual(
gt_operation.describe(), 'Remove constraint test_constraint_pony_pink_for_weight_gt_5_uniq from model Pony'
)
# Test state alteration
new_state = project_state.clone()
gt_operation.state_forwards('test_removepartialuniqueconstraint', new_state)
self.assertEqual(len(new_state.models['test_removepartialuniqueconstraint', 'pony'].options['constraints']), 0)
Pony = new_state.apps.get_model('test_removepartialuniqueconstraint', 'Pony')
self.assertEqual(len(Pony._meta.constraints), 0)
# Test database alteration
with connection.schema_editor() as editor:
gt_operation.database_forwards('test_removepartialuniqueconstraint', editor, project_state, new_state)
# Test constraint doesn't work
Pony.objects.create(pink=1, weight=4.0)
Pony.objects.create(pink=1, weight=4.0)
Pony.objects.create(pink=1, weight=6.0)
Pony.objects.create(pink=1, weight=7.0).delete()
# Test reversal
with connection.schema_editor() as editor:
gt_operation.database_backwards('test_removepartialuniqueconstraint', editor, new_state, project_state)
# Test constraint works
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 deconstruction
definition = gt_operation.deconstruct()
self.assertEqual(definition[0], 'RemoveConstraint')
self.assertEqual(definition[1], [])
self.assertEqual(definition[2], {
'model_name': 'Pony',
'name': 'test_constraint_pony_pink_for_weight_gt_5_uniq',
})