mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Refs #29641 -- Refactored database schema constraint creation.
Added a test for constraint names in the database. Updated SQLite introspection to use sqlparse to allow reading the constraint name for table check and unique constraints. Co-authored-by: Ian Foote <python@ian.feete.org>
This commit is contained in:
parent
2f120ac517
commit
dba4a634ba
7 changed files with 147 additions and 82 deletions
|
@ -1,10 +1,15 @@
|
|||
from django.db import IntegrityError, models
|
||||
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
|
||||
|
||||
|
||||
def get_constraints(table):
|
||||
with connection.cursor() as cursor:
|
||||
return connection.introspection.get_constraints(cursor, table)
|
||||
|
||||
|
||||
class BaseConstraintTests(SimpleTestCase):
|
||||
def test_constraint_sql(self):
|
||||
c = BaseConstraint('name')
|
||||
|
@ -37,3 +42,11 @@ class CheckConstraintTests(TestCase):
|
|||
Product.objects.create(name='Valid', price=10, discounted_price=5)
|
||||
with self.assertRaises(IntegrityError):
|
||||
Product.objects.create(name='Invalid', price=10, discounted_price=20)
|
||||
|
||||
@skipUnlessDBFeature('supports_table_check_constraints')
|
||||
def test_name(self):
|
||||
constraints = get_constraints(Product._meta.db_table)
|
||||
expected_name = 'price_gt_discounted_price'
|
||||
if connection.features.uppercases_column_names:
|
||||
expected_name = expected_name.upper()
|
||||
self.assertIn(expected_name, constraints)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue