mirror of
https://github.com/django/django.git
synced 2025-10-25 06:36:07 +00:00
Don't make a second migration if there was a force-null-default addcol.
This commit is contained in:
@@ -188,15 +188,26 @@ class MigrationAutodetector(object):
|
||||
continue
|
||||
# You can't just add NOT NULL fields with no default
|
||||
if not field.null and not field.has_default():
|
||||
field = field.clone()
|
||||
field.default = self.questioner.ask_not_null_addition(field_name, model_name)
|
||||
self.add_to_migration(
|
||||
app_label,
|
||||
operations.AddField(
|
||||
model_name=model_name,
|
||||
name=field_name,
|
||||
field=field,
|
||||
self.add_to_migration(
|
||||
app_label,
|
||||
operations.AddField(
|
||||
model_name=model_name,
|
||||
name=field_name,
|
||||
field=field,
|
||||
preserve_default=False,
|
||||
)
|
||||
)
|
||||
else:
|
||||
self.add_to_migration(
|
||||
app_label,
|
||||
operations.AddField(
|
||||
model_name=model_name,
|
||||
name=field_name,
|
||||
field=field,
|
||||
)
|
||||
)
|
||||
)
|
||||
# Old fields
|
||||
for field_name in old_field_names - new_field_names:
|
||||
self.add_to_migration(
|
||||
@@ -434,7 +445,8 @@ class InteractiveMigrationQuestioner(MigrationQuestioner):
|
||||
"Adding a NOT NULL field to a model"
|
||||
choice = self._choice_input(
|
||||
"You are trying to add a non-nullable field '%s' to %s without a default;\n" % (field_name, model_name) +
|
||||
"this is not possible. Please select a fix:",
|
||||
"we can't do that (the database needs something to populate existing rows).\n" +
|
||||
"Please select a fix:",
|
||||
[
|
||||
"Provide a one-off default now (will be set on all existing rows)",
|
||||
"Quit, and let me add a default in models.py",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from django.db import router
|
||||
from django.db.models.fields import NOT_PROVIDED
|
||||
from .base import Operation
|
||||
|
||||
|
||||
@@ -7,13 +8,20 @@ class AddField(Operation):
|
||||
Adds a field to a model.
|
||||
"""
|
||||
|
||||
def __init__(self, model_name, name, field):
|
||||
def __init__(self, model_name, name, field, preserve_default=True):
|
||||
self.model_name = model_name
|
||||
self.name = name
|
||||
self.field = field
|
||||
self.preserve_default = preserve_default
|
||||
|
||||
def state_forwards(self, app_label, state):
|
||||
state.models[app_label, self.model_name.lower()].fields.append((self.name, self.field))
|
||||
# If preserve default is off, don't use the default for future state
|
||||
if not self.preserve_default:
|
||||
field = self.field.clone()
|
||||
field.default = NOT_PROVIDED
|
||||
else:
|
||||
field = self.field
|
||||
state.models[app_label, self.model_name.lower()].fields.append((self.name, field))
|
||||
|
||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||
from_model = from_state.render().get_model(app_label, self.model_name)
|
||||
|
||||
Reference in New Issue
Block a user