summaryrefslogtreecommitdiff
path: root/django/db/models/constraints.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2018-08-05 21:06:52 -0400
committerTim Graham <timograham@gmail.com>2018-11-13 15:25:44 -0500
commitdba4a634ba999bf376caee193b3378bc0b730bd4 (patch)
treedcf309133def892bc314e69f423e926f6e4dcc73 /django/db/models/constraints.py
parent2f120ac51722a257219a7577759702605cefddf4 (diff)
downloaddjango-dba4a634ba999bf376caee193b3378bc0b730bd4.tar.gz
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>
Diffstat (limited to 'django/db/models/constraints.py')
-rw-r--r--django/db/models/constraints.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/django/db/models/constraints.py b/django/db/models/constraints.py
index 2bad8db221..698b278fe8 100644
--- a/django/db/models/constraints.py
+++ b/django/db/models/constraints.py
@@ -10,16 +10,22 @@ class BaseConstraint:
def constraint_sql(self, model, schema_editor):
raise NotImplementedError('This method must be implemented by a subclass.')
+ def full_constraint_sql(self, model, schema_editor):
+ return schema_editor.sql_constraint % {
+ 'name': schema_editor.quote_name(self.name),
+ 'constraint': self.constraint_sql(model, schema_editor),
+ }
+
def create_sql(self, model, schema_editor):
- sql = self.constraint_sql(model, schema_editor)
- return schema_editor.sql_create_check % {
+ sql = self.full_constraint_sql(model, schema_editor)
+ return schema_editor.sql_create_constraint % {
'table': schema_editor.quote_name(model._meta.db_table),
- 'check': sql,
+ 'constraint': sql,
}
def remove_sql(self, model, schema_editor):
quote_name = schema_editor.quote_name
- return schema_editor.sql_delete_check % {
+ return schema_editor.sql_delete_constraint % {
'table': quote_name(model._meta.db_table),
'name': quote_name(self.name),
}
@@ -46,10 +52,7 @@ class CheckConstraint(BaseConstraint):
compiler = connection.ops.compiler('SQLCompiler')(query, connection, 'default')
sql, params = where.as_sql(compiler, connection)
params = tuple(schema_editor.quote_value(p) for p in params)
- return schema_editor.sql_check % {
- 'name': schema_editor.quote_name(self.name),
- 'check': sql % params,
- }
+ return schema_editor.sql_check_constraint % {'check': sql % params}
def __repr__(self):
return "<%s: check='%s' name=%r>" % (self.__class__.__name__, self.check, self.name)