mirror of
https://github.com/django/django.git
synced 2025-05-17 12:26:29 +00:00
Fixed #29517 -- Added support for SQLite column check constraints on positive integer fields.
This commit is contained in:
parent
7410618528
commit
b9cf764be6
@ -76,6 +76,10 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
|||||||
'TimeField': 'time',
|
'TimeField': 'time',
|
||||||
'UUIDField': 'char(32)',
|
'UUIDField': 'char(32)',
|
||||||
}
|
}
|
||||||
|
data_type_check_constraints = {
|
||||||
|
'PositiveIntegerField': '"%(column)s" >= 0',
|
||||||
|
'PositiveSmallIntegerField': '"%(column)s" >= 0',
|
||||||
|
}
|
||||||
data_types_suffix = {
|
data_types_suffix = {
|
||||||
'AutoField': 'AUTOINCREMENT',
|
'AutoField': 'AUTOINCREMENT',
|
||||||
'BigAutoField': 'AUTOINCREMENT',
|
'BigAutoField': 'AUTOINCREMENT',
|
||||||
|
@ -14,7 +14,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
|
|||||||
supports_timezones = False
|
supports_timezones = False
|
||||||
max_query_params = 999
|
max_query_params = 999
|
||||||
supports_mixed_date_datetime_comparisons = False
|
supports_mixed_date_datetime_comparisons = False
|
||||||
supports_column_check_constraints = False
|
|
||||||
autocommits_when_autocommit_is_off = True
|
autocommits_when_autocommit_is_off = True
|
||||||
can_introspect_decimal_field = False
|
can_introspect_decimal_field = False
|
||||||
can_introspect_positive_integer_field = True
|
can_introspect_positive_integer_field = True
|
||||||
|
@ -231,6 +231,32 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
|
|||||||
one or more columns.
|
one or more columns.
|
||||||
"""
|
"""
|
||||||
constraints = {}
|
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
|
# Get the index info
|
||||||
cursor.execute("PRAGMA index_list(%s)" % self.connection.ops.quote_name(table_name))
|
cursor.execute("PRAGMA index_list(%s)" % self.connection.ops.quote_name(table_name))
|
||||||
for row in cursor.fetchall():
|
for row in cursor.fetchall():
|
||||||
|
@ -218,7 +218,10 @@ Database backend API
|
|||||||
Miscellaneous
|
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:
|
.. _deprecated-features-2.2:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user