summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2014-01-19 17:42:29 +0000
committerAndrew Godwin <andrew@aeracode.org>2014-01-19 17:43:31 +0000
commitaf4a8478e4beaa5c870620d6b5a2555eaed8b850 (patch)
tree0ccb662a0742919c8a12213ddb47589fa18df29d
parente8d4aed3b97493a050f69dca1f6562991d5c511b (diff)
Fixed #21783: (again) Found second source of bug, also squashed it.
-rw-r--r--django/db/migrations/operations/fields.py10
-rw-r--r--tests/migrations/test_operations.py8
2 files changed, 17 insertions, 1 deletions
diff --git a/django/db/migrations/operations/fields.py b/django/db/migrations/operations/fields.py
index c5f0bd1e2b..a123d0b057 100644
--- a/django/db/migrations/operations/fields.py
+++ b/django/db/migrations/operations/fields.py
@@ -27,7 +27,15 @@ class AddField(Operation):
from_model = from_state.render().get_model(app_label, self.model_name)
to_model = to_state.render().get_model(app_label, self.model_name)
if router.allow_migrate(schema_editor.connection.alias, to_model):
- schema_editor.add_field(from_model, to_model._meta.get_field_by_name(self.name)[0])
+ field = to_model._meta.get_field_by_name(self.name)[0]
+ if not self.preserve_default:
+ field.default = self.field.default
+ schema_editor.add_field(
+ from_model,
+ field,
+ )
+ if not self.preserve_default:
+ field.default = NOT_PROVIDED
def database_backwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.render().get_model(app_label, self.model_name)
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index d11fda23c8..f60c1a27e7 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -184,6 +184,14 @@ class OperationTests(MigrationTestBase):
if n == "height"
][0]
self.assertEqual(field.default, NOT_PROVIDED)
+ # Test the database alteration
+ project_state.render().get_model("test_adflpd", "pony").objects.create(
+ weight = 4,
+ )
+ self.assertColumnNotExists("test_adflpd_pony", "height")
+ with connection.schema_editor() as editor:
+ operation.database_forwards("test_adflpd", editor, project_state, new_state)
+ self.assertColumnExists("test_adflpd_pony", "height")
def test_add_field_m2m(self):
"""