Fixed #23416 -- Make sure DatabaseCreation respects checks.

Migrations respected Field.db_parameters()['check'], but
DatabaseCreation was still using just Field.db_type().
This commit is contained in:
Marc Tamlyn 2014-09-04 16:32:51 +01:00
parent e5febfb3c3
commit 14c8456ffe
4 changed files with 21 additions and 1 deletions

View File

@ -77,7 +77,10 @@ class BaseDatabaseCreation(object):
pending_references = {} pending_references = {}
qn = self.connection.ops.quote_name qn = self.connection.ops.quote_name
for f in opts.local_fields: for f in opts.local_fields:
col_type = f.db_type(connection=self.connection) db_params = f.db_parameters(connection=self.connection)
col_type = db_params['type']
if db_params['check']:
col_type = '%s CHECK (%s)' % (col_type, db_params['check'])
col_type_suffix = f.db_type_suffix(connection=self.connection) col_type_suffix = f.db_type_suffix(connection=self.connection)
tablespace = f.db_tablespace or opts.db_tablespace tablespace = f.db_tablespace or opts.db_tablespace
if col_type is None: if col_type is None:

View File

@ -13,3 +13,6 @@ Bugfixes
* Fixed a ``UnicodeDecodeError`` when the ``flush`` error message contained * Fixed a ``UnicodeDecodeError`` when the ``flush`` error message contained
Unicode characters (:ticket:`22882`). Unicode characters (:ticket:`22882`).
* Reinstated missing ``CHECK`` SQL clauses which were omitted on some backends
when not using migrations (:ticket:`23416`).

View File

@ -8,3 +8,4 @@ class Comment(models.Model):
class Book(models.Model): class Book(models.Model):
title = models.CharField(max_length=100, db_index=True) title = models.CharField(max_length=100, db_index=True)
comments = models.ManyToManyField(Comment) comments = models.ManyToManyField(Comment)
counter = models.PositiveIntegerField()

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import re import re
import unittest
from django.apps import apps from django.apps import apps
from django.core.management.color import no_style from django.core.management.color import no_style
@ -43,6 +44,18 @@ class SQLCommandsTestCase(TestCase):
'commands_sql_comment', 'commands_sql_book', 'commands_sql_book_comments' 'commands_sql_comment', 'commands_sql_book', 'commands_sql_book_comments'
}) })
@unittest.skipUnless('PositiveIntegerField' in connections[DEFAULT_DB_ALIAS].creation.data_type_check_constraints, 'Backend does not have checks.')
def test_sql_create_check(self):
"""Regression test for #23416 -- Check that db_params['check'] is respected."""
app_config = apps.get_app_config('commands_sql')
output = sql_create(app_config, no_style(), connections[DEFAULT_DB_ALIAS])
success = False
for statement in output:
if 'CHECK' in statement:
success = True
if not success:
self.fail("'CHECK' not found in ouput %s" % output)
def test_sql_delete(self): def test_sql_delete(self):
app_config = apps.get_app_config('commands_sql') app_config = apps.get_app_config('commands_sql')
output = sql_delete(app_config, no_style(), connections[DEFAULT_DB_ALIAS], close_connection=False) output = sql_delete(app_config, no_style(), connections[DEFAULT_DB_ALIAS], close_connection=False)