1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #29517 -- Added support for SQLite column check constraints on positive integer fields.

This commit is contained in:
Tim Graham 2018-06-22 14:42:51 -04:00
parent 7410618528
commit b9cf764be6
4 changed files with 34 additions and 2 deletions

View File

@ -76,6 +76,10 @@ class DatabaseWrapper(BaseDatabaseWrapper):
'TimeField': 'time',
'UUIDField': 'char(32)',
}
data_type_check_constraints = {
'PositiveIntegerField': '"%(column)s" >= 0',
'PositiveSmallIntegerField': '"%(column)s" >= 0',
}
data_types_suffix = {
'AutoField': 'AUTOINCREMENT',
'BigAutoField': 'AUTOINCREMENT',

View File

@ -14,7 +14,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_timezones = False
max_query_params = 999
supports_mixed_date_datetime_comparisons = False
supports_column_check_constraints = False
autocommits_when_autocommit_is_off = True
can_introspect_decimal_field = False
can_introspect_positive_integer_field = True

View File

@ -231,6 +231,32 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
one or more columns.
"""
constraints = {}
# Find inline check constraints.
try:
table_schema = cursor.execute(
"SELECT sql FROM sqlite_master WHERE type='table' and name=%s" % (
self.connection.ops.quote_name(table_name),
)
).fetchone()[0]
except TypeError:
# table_name is a view.
pass
else:
fields_with_check_constraints = [
schema_row.strip().split(' ')[0][1:-1]
for schema_row in table_schema.split(',')
if schema_row.find('CHECK') >= 0
]
for field_name in fields_with_check_constraints:
# An arbitrary made up name.
constraints['__check__%s' % field_name] = {
'columns': [field_name],
'primary_key': False,
'unique': False,
'foreign_key': False,
'check': True,
'index': False,
}
# Get the index info
cursor.execute("PRAGMA index_list(%s)" % self.connection.ops.quote_name(table_name))
for row in cursor.fetchall():

View File

@ -218,7 +218,10 @@ Database backend API
Miscellaneous
-------------
* ...
* On SQLite, ``PositiveIntegerField`` and ``PositiveSmallIntegerField`` now
include a check constraint to prevent negative values in the database. If you
have existing invalid data and run a migration that recreates a table, you'll
see ``CHECK constraint failed``.
.. _deprecated-features-2.2: