mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #30397 -- Added app_label/class interpolation for names of indexes and constraints.
This commit is contained in:
parent
8233144ca0
commit
febe136d4c
9 changed files with 166 additions and 11 deletions
|
@ -13,6 +13,10 @@ class Product(models.Model):
|
|||
check=models.Q(price__gt=models.F('discounted_price')),
|
||||
name='price_gt_discounted_price',
|
||||
),
|
||||
models.CheckConstraint(
|
||||
check=models.Q(price__gt=0),
|
||||
name='%(app_label)s_%(class)s_price_gt_0',
|
||||
),
|
||||
models.UniqueConstraint(fields=['name', 'color'], name='name_color_uniq'),
|
||||
models.UniqueConstraint(
|
||||
fields=['name'],
|
||||
|
@ -20,3 +24,20 @@ class Product(models.Model):
|
|||
condition=models.Q(color__isnull=True),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
class AbstractModel(models.Model):
|
||||
age = models.IntegerField()
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
constraints = [
|
||||
models.CheckConstraint(
|
||||
check=models.Q(age__gte=18),
|
||||
name='%(app_label)s_%(class)s_adult',
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
class ChildModel(AbstractModel):
|
||||
pass
|
||||
|
|
|
@ -3,7 +3,7 @@ from django.db import IntegrityError, connection, models
|
|||
from django.db.models.constraints import BaseConstraint
|
||||
from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
|
||||
|
||||
from .models import Product
|
||||
from .models import ChildModel, Product
|
||||
|
||||
|
||||
def get_constraints(table):
|
||||
|
@ -76,8 +76,17 @@ class CheckConstraintTests(TestCase):
|
|||
@skipUnlessDBFeature('supports_table_check_constraints')
|
||||
def test_name(self):
|
||||
constraints = get_constraints(Product._meta.db_table)
|
||||
expected_name = 'price_gt_discounted_price'
|
||||
self.assertIn(expected_name, constraints)
|
||||
for expected_name in (
|
||||
'price_gt_discounted_price',
|
||||
'constraints_product_price_gt_0',
|
||||
):
|
||||
with self.subTest(expected_name):
|
||||
self.assertIn(expected_name, constraints)
|
||||
|
||||
@skipUnlessDBFeature('supports_table_check_constraints')
|
||||
def test_abstract_name(self):
|
||||
constraints = get_constraints(ChildModel._meta.db_table)
|
||||
self.assertIn('constraints_childmodel_adult', constraints)
|
||||
|
||||
|
||||
class UniqueConstraintTests(TestCase):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue