summaryrefslogtreecommitdiff
path: root/django/db/backends/sqlite3
diff options
context:
space:
mode:
authorIan Foote <python@ian.feete.org>2020-11-22 22:27:57 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-05-12 19:11:40 +0200
commit7414704e88d73dafbcfbb85f9bc54cb6111439d3 (patch)
treef51136b16e457d7f46e01ff3cc06308faf0923db /django/db/backends/sqlite3
parent599f3e2cda50ab084915ffd08edb5ad6cad61415 (diff)
Fixed #470 -- Added support for database defaults on fields.
Special thanks to Hannes Ljungberg for finding multiple implementation gaps. Thanks also to Simon Charette, Adam Johnson, and Mariusz Felisiak for reviews.
Diffstat (limited to 'django/db/backends/sqlite3')
-rw-r--r--django/db/backends/sqlite3/features.py2
-rw-r--r--django/db/backends/sqlite3/schema.py24
2 files changed, 22 insertions, 4 deletions
diff --git a/django/db/backends/sqlite3/features.py b/django/db/backends/sqlite3/features.py
index 7dd1c39702..f471b72cb2 100644
--- a/django/db/backends/sqlite3/features.py
+++ b/django/db/backends/sqlite3/features.py
@@ -59,6 +59,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
PRIMARY KEY(column_1, column_2)
)
"""
+ insert_test_table_with_defaults = 'INSERT INTO {} ("null") VALUES (1)'
+ supports_default_keyword_in_insert = False
@cached_property
def django_test_skips(self):
diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py
index 2ca9a01855..46ba07092d 100644
--- a/django/db/backends/sqlite3/schema.py
+++ b/django/db/backends/sqlite3/schema.py
@@ -6,7 +6,7 @@ from django.db import NotSupportedError
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.backends.ddl_references import Statement
from django.db.backends.utils import strip_quotes
-from django.db.models import UniqueConstraint
+from django.db.models import NOT_PROVIDED, UniqueConstraint
from django.db.transaction import atomic
@@ -233,9 +233,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
if create_field:
body[create_field.name] = create_field
# Choose a default and insert it into the copy map
- if not create_field.many_to_many and create_field.concrete:
+ if (
+ create_field.db_default is NOT_PROVIDED
+ and not create_field.many_to_many
+ and create_field.concrete
+ ):
mapping[create_field.column] = self.prepare_default(
- self.effective_default(create_field),
+ self.effective_default(create_field)
)
# Add in any altered fields
for alter_field in alter_fields:
@@ -244,9 +248,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
mapping.pop(old_field.column, None)
body[new_field.name] = new_field
if old_field.null and not new_field.null:
+ if new_field.db_default is NOT_PROVIDED:
+ default = self.prepare_default(self.effective_default(new_field))
+ else:
+ default, _ = self.db_default_sql(new_field)
case_sql = "coalesce(%(col)s, %(default)s)" % {
"col": self.quote_name(old_field.column),
- "default": self.prepare_default(self.effective_default(new_field)),
+ "default": default,
}
mapping[new_field.column] = case_sql
else:
@@ -381,6 +389,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
def add_field(self, model, field):
"""Create a field on a model."""
+ from django.db.models.expressions import Value
+
# Special-case implicit M2M tables.
if field.many_to_many and field.remote_field.through._meta.auto_created:
self.create_model(field.remote_field.through)
@@ -394,6 +404,12 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# COLUMN statement because DROP DEFAULT is not supported in
# ALTER TABLE.
or self.effective_default(field) is not None
+ # Fields with non-constant defaults cannot by handled by ALTER
+ # TABLE ADD COLUMN statement.
+ or (
+ field.db_default is not NOT_PROVIDED
+ and not isinstance(field.db_default, Value)
+ )
):
self._remake_table(model, create_field=field)
else: