diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2013-12-04 13:56:22 +0000 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2013-12-04 13:56:22 +0000 |
| commit | ce05b8a69ee0e9f7f7e0154b2b9bf1dcb15edbcb (patch) | |
| tree | 47f5f4741b969af1d923a57e2077c3d490b04b17 /django | |
| parent | df800b160990884246d73786e85b082d9b703b57 (diff) | |
Don't make a second migration if there was a force-null-default addcol.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/migrations/autodetector.py | 28 | ||||
| -rw-r--r-- | django/db/migrations/operations/fields.py | 12 |
2 files changed, 30 insertions, 10 deletions
diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 083351a6f1..f8ec49ab0a 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -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", diff --git a/django/db/migrations/operations/fields.py b/django/db/migrations/operations/fields.py index b609110f85..c5f0bd1e2b 100644 --- a/django/db/migrations/operations/fields.py +++ b/django/db/migrations/operations/fields.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) |
