[Django] #23983: Cannot Alter order_with_respect_to after there is already data in a table

7 views
Skip to first unread message

Django

unread,
Dec 11, 2014, 5:53:30 PM12/11/14
to django-...@googlegroups.com
#23983: Cannot Alter order_with_respect_to after there is already data in a table
----------------------------+--------------------
Reporter: jpulec | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
Due to the way that migration operation 'AlterOrderWithRespectTo' is
implemented, it will not set a default for the '_order' column it tries to
create. Because of this, it is not possible to change
'order_with_respect_to' once there is already data in your database, since
the SQL it generates is 'ALTER TABLE "test_app_eggs" ADD COLUMN "_order"
integer NOT NULL;'.


Inital models:
{{{
from django.db import models

# Create your models here.


class Spam(models.Model):
name = models.CharField(max_length=200)


class Eggs(models.Model):
spam = models.ForeignKey('test_app.Spam')
name = models.CharField(max_length=200)

}}}

Then add some data, and change the models to:


{{{
from django.db import models

# Create your models here.


class Spam(models.Model):
name = models.CharField(max_length=200)


class Eggs(models.Model):
spam = models.ForeignKey('test_app.Spam')
name = models.CharField(max_length=200)

class Meta:
order_with_respect_to = 'spam'

}}}

and then make migrations and try to migrate results in the following
traceback:


{{{
(test_project)~/test_project/test_project: $ python manage.py migrate
Operations to perform:
Apply all migrations: admin, test_app, contenttypes, auth, sessions
Running migrations:
Applying test_app.0002_auto_20141211_2202...Traceback (most recent call
last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 385, in
execute_from_command_line
utility.execute()
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor,
project_state, new_state)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/migrations/operations/models.py", line 328, in
database_forwards
field,
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/backends/schema.py", line 390, in add_field
self.execute(sql, params)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/backends/schema.py", line 99, in execute
cursor.execute(sql, params)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/james/.virtualenvs/test_project/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: column "_order" contains null values
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23983>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 12, 2014, 3:41:43 PM12/12/14
to django-...@googlegroups.com
#23983: Cannot Alter order_with_respect_to after there is already data in a table
----------------------------+------------------------------------

Reporter: jpulec | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/23983#comment:1>

Django

unread,
Dec 15, 2014, 5:50:57 PM12/15/14
to django-...@googlegroups.com
#23983: Cannot Alter order_with_respect_to after there is already data in a table
----------------------------+------------------------------------
Reporter: jpulec | Owner: coldmind
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by coldmind):

* status: new => assigned
* owner: nobody => coldmind


Comment:

I'll try to fix that

--
Ticket URL: <https://code.djangoproject.com/ticket/23983#comment:2>

Django

unread,
Dec 15, 2014, 6:01:59 PM12/15/14
to django-...@googlegroups.com
#23983: Cannot Alter order_with_respect_to after there is already data in a table
----------------------------+------------------------------------
Reporter: jpulec | Owner: coldmind
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------

Comment (by coldmind):

South sets 0 as default value for new created `_order` column, i think in
that case it should be the same.
Or, maybe it needed a new questioner? But it seems to will have no profit.

--
Ticket URL: <https://code.djangoproject.com/ticket/23983#comment:3>

Django

unread,
Dec 15, 2014, 6:04:34 PM12/15/14
to django-...@googlegroups.com
#23983: Cannot Alter order_with_respect_to after there is already data in a table
----------------------------+------------------------------------
Reporter: jpulec | Owner: coldmind
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------

Comment (by timgraham):

Yes, I don't think we need a modification to the questioner.

--
Ticket URL: <https://code.djangoproject.com/ticket/23983#comment:4>

Django

unread,
Dec 16, 2014, 4:55:23 AM12/16/14
to django-...@googlegroups.com
#23983: Cannot Alter order_with_respect_to after there is already data in a table
----------------------------+------------------------------------
Reporter: jpulec | Owner: coldmind
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by coldmind):

* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/3735

--
Ticket URL: <https://code.djangoproject.com/ticket/23983#comment:5>

Django

unread,
Dec 16, 2014, 5:03:41 AM12/16/14
to django-...@googlegroups.com
#23983: Cannot Alter order_with_respect_to after there is already data in a table
----------------------------+------------------------------------
Reporter: jpulec | Owner: coldmind
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------

Comment (by MarkusH):

I think we should set an initial ordering
(https://github.com/django/django/blob/aa5ef0d4fc67a95ac2a5103810d0c87d8c547bac/django/db/models/base.py#L1624-L1633)
. Otherwise upcoming saves will start to be ordered, while old data isn't.
The ordering of the existing data should be done by the default ordering
of the referenced model (`model._meta.ordering`)

--
Ticket URL: <https://code.djangoproject.com/ticket/23983#comment:6>

Django

unread,
Dec 16, 2014, 10:49:15 AM12/16/14
to django-...@googlegroups.com
#23983: Cannot Alter order_with_respect_to after there is already data in a table
----------------------------+------------------------------------
Reporter: jpulec | Owner: coldmind
Type: Bug | Status: closed
Component: Migrations | Version: 1.7
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"3dbbb8a89ca4beaabd5359fe82e32ed633b15140"]:
{{{
#!CommitTicketReference repository=""
revision="3dbbb8a89ca4beaabd5359fe82e32ed633b15140"
Fixed #23983 -- Fixed a crash in migrations when adding
order_with_respect_to to non-empty table.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23983#comment:7>

Django

unread,
Dec 16, 2014, 10:59:16 AM12/16/14
to django-...@googlegroups.com
#23983: Cannot Alter order_with_respect_to after there is already data in a table
----------------------------+------------------------------------
Reporter: jpulec | Owner: coldmind
Type: Bug | Status: closed
Component: Migrations | Version: 1.7

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"10482faf1996e654cb9849d7c0065841ad2adf35"]:
{{{
#!CommitTicketReference repository=""
revision="10482faf1996e654cb9849d7c0065841ad2adf35"
[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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23983#comment:8>

Reply all
Reply to author
Forward
0 new messages