Fixed #23983 -- Fixed a crash in migrations when adding order_with_respect_to to non-empty table.

This commit is contained in:
Andriy Sokolovskiy 2014-12-16 02:22:28 +02:00 committed by Tim Graham
parent ca013e652f
commit 3dbbb8a89c
4 changed files with 15 additions and 0 deletions

View File

@ -47,6 +47,7 @@ answer newbie questions, and generally made Django that much better:
Andrew Godwin <andrew@aeracode.org> Andrew Godwin <andrew@aeracode.org>
Andrew Pinkham <http://AndrewsForge.com> Andrew Pinkham <http://AndrewsForge.com>
Andrews Medina <andrewsmedina@gmail.com> Andrews Medina <andrewsmedina@gmail.com>
Andriy Sokolovskiy <sokandpal@yandex.ru>
Andy Dustman <farcepest@gmail.com> Andy Dustman <farcepest@gmail.com>
Andy Gayton <andy-django@thecablelounge.com> Andy Gayton <andy-django@thecablelounge.com>
andy@jadedplanet.net andy@jadedplanet.net

View File

@ -404,6 +404,8 @@ class AlterOrderWithRespectTo(Operation):
# it's likely a rename) # it's likely a rename)
elif to_model._meta.order_with_respect_to and not from_model._meta.order_with_respect_to: 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] field = to_model._meta.get_field_by_name("_order")[0]
if not field.has_default():
field.default = 0
schema_editor.add_field( schema_editor.add_field(
from_model, from_model,
field, field,

View File

@ -143,3 +143,6 @@ Bugfixes
* ``makemigrations`` no longer prompts for a default value when adding * ``makemigrations`` no longer prompts for a default value when adding
``TextField()`` or ``CharField()`` without a ``default`` (:ticket:`23405`). ``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`).

View File

@ -1237,10 +1237,19 @@ class OperationTests(OperationTestBase):
self.assertEqual(new_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None), "pony") self.assertEqual(new_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None), "pony")
# Make sure there's no matching index # Make sure there's no matching index
self.assertColumnNotExists("test_alorwrtto_rider", "_order") 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 # Test the database alteration
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
operation.database_forwards("test_alorwrtto", editor, project_state, new_state) operation.database_forwards("test_alorwrtto", editor, project_state, new_state)
self.assertColumnExists("test_alorwrtto_rider", "_order") 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 # And test reversal
with connection.schema_editor() as editor: with connection.schema_editor() as editor:
operation.database_backwards("test_alorwrtto", editor, new_state, project_state) operation.database_backwards("test_alorwrtto", editor, new_state, project_state)