diff options
| author | Andriy Sokolovskiy <sokandpal@yandex.ru> | 2014-12-16 02:22:28 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-12-16 10:56:35 -0500 |
| commit | 10482faf1996e654cb9849d7c0065841ad2adf35 (patch) | |
| tree | a18517568dc4da497a24f9a09a142c8539331a3e | |
| parent | 66c0529b3edb40e25713cbf32ea8794befc829ae (diff) | |
[1.7.x] Fixed #23983 -- Fixed a crash in migrations when adding order_with_respect_to to non-empty table.
Backport of 3dbbb8a89ca4beaabd5359fe82e32ed633b15140 from master
| -rw-r--r-- | django/db/migrations/operations/models.py | 2 | ||||
| -rw-r--r-- | docs/releases/1.7.2.txt | 3 | ||||
| -rw-r--r-- | tests/migrations/test_operations.py | 9 |
3 files changed, 14 insertions, 0 deletions
diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index e195d9d841..75053474a9 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -329,6 +329,8 @@ class AlterOrderWithRespectTo(Operation): # it's likely a rename) elif to_model._meta.order_with_respect_to and not from_model._meta.order_with_respect_to: field = to_model._meta.get_field_by_name("_order")[0] + if not field.has_default(): + field.default = 0 schema_editor.add_field( from_model, field, diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt index ea086490cf..dedf92c8cb 100644 --- a/docs/releases/1.7.2.txt +++ b/docs/releases/1.7.2.txt @@ -143,3 +143,6 @@ Bugfixes * ``makemigrations`` no longer prompts for a default value when adding ``TextField()`` or ``CharField()`` without a ``default`` (:ticket:`23405`). + +* Fixed migration crash when adding ``order_with_respect_to`` to a table + with existing rows (:ticket:`23983`). diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 5f4dc553a2..ed4bf9423d 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -1129,10 +1129,19 @@ class OperationTests(OperationTestBase): self.assertEqual(new_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None), "pony") # Make sure there's no matching index self.assertColumnNotExists("test_alorwrtto_rider", "_order") + # Create some rows before alteration + rendered_state = project_state.render() + pony = rendered_state.get_model("test_alorwrtto", "Pony").objects.create(weight=50) + rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony, friend_id=1) + rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony, friend_id=2) # Test the database alteration with connection.schema_editor() as editor: operation.database_forwards("test_alorwrtto", editor, project_state, new_state) self.assertColumnExists("test_alorwrtto_rider", "_order") + # Check for correct value in rows + updated_riders = new_state.render().get_model("test_alorwrtto", "Rider").objects.all() + self.assertEqual(updated_riders[0]._order, 0) + self.assertEqual(updated_riders[1]._order, 0) # And test reversal with connection.schema_editor() as editor: operation.database_backwards("test_alorwrtto", editor, new_state, project_state) |
