[Django] #22470: Migrations fail with order_with_respect_to and ForeignKey to self

30 views
Skip to first unread message

Django

unread,
Apr 18, 2014, 8:25:29 AM4/18/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
----------------------------+----------------------------------------------
Reporter: Jonas | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-beta-1
Severity: Normal | Keywords: migrations order_with_respect_to
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+----------------------------------------------
Initial migrations for an app with this kind of model setup will fail when
running `./manage.py migrate`:

{{{#!python
class Parent(models.Model):
spouse = models.ForeignKey('self')

class Child(models.Model):
parent = models.ForeignKey(Parent)

class Meta:
order_with_respect_to = 'parent'
}}}

This is the resulting traceback:

{{{
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/jonas/temp/django/django/core/management/__init__.py", line
427, in execute_from_command_line
utility.execute()
File "/home/jonas/temp/django/django/core/management/__init__.py", line
419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/jonas/temp/django/django/core/management/base.py", line 288,
in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/jonas/temp/django/django/core/management/base.py", line 337,
in execute
output = self.handle(*args, **options)
File
"/home/jonas/temp/django/django/core/management/commands/migrate.py", line
145, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/jonas/temp/django/django/db/migrations/executor.py", line
60, in migrate
self.apply_migration(migration, fake=fake)
File "/home/jonas/temp/django/django/db/migrations/executor.py", line
88, in apply_migration
if self.detect_soft_applied(migration):
File "/home/jonas/temp/django/django/db/migrations/executor.py", line
132, in detect_soft_applied
apps = project_state.render()
File "/home/jonas/temp/django/django/db/migrations/state.py", line 48,
in render
model.render(self.apps)
File "/home/jonas/temp/django/django/db/migrations/state.py", line 232,
in render
body,
File "/home/jonas/temp/django/django/db/models/base.py", line 298, in
__new__
new_class._prepare()
File "/home/jonas/temp/django/django/db/models/base.py", line 322, in
_prepare
opts._prepare(cls)
File "/home/jonas/temp/django/django/db/models/options.py", line 180, in
_prepare
self.order_with_respect_to =
self.get_field(self.order_with_respect_to)
File "/home/jonas/temp/django/django/db/models/options.py", line 397, in
get_field
raise FieldDoesNotExist('%s has no field named %r' %
(self.object_name, name))
django.db.models.fields.FieldDoesNotExist: Child has no field named
'parent'
}}}

`makemigrations` creates three migrations:

* The first migration creates the `Child` model ''without'' the `parent`
field as well as the `Parent` model without the `spouse` field.
* The second migration adds the `spouse` field to `Parent`.
* The third migration adds `parent` to `Child`.

The error occurs in the first migration, when the `_prepare()` wants to
access the `parent` field for `order_with_respect_to` and can't, as it
doesn't exist yet.

I tried adding a test in Django proper but wasn't able to. Will keep
trying but support is appreciated.

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

Django

unread,
Apr 22, 2014, 4:48:21 AM4/22/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------

Reporter: Jonas | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version:
Severity: Normal | 1.7-beta-1
Keywords: migrations | Resolution:
order_with_respect_to | Triage Stage:
Has patch: 0 | Unreviewed
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jonas):

* cc: Jonas (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


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

Django

unread,
Apr 28, 2014, 8:15:24 PM4/28/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------

Reporter: Jonas | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version:
Severity: Release blocker | 1.7-beta-1
Keywords: migrations | Resolution:
order_with_respect_to | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


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

Django

unread,
May 7, 2014, 1:48:09 AM5/7/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------
Reporter: Jonas | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: assigned
Severity: Release blocker | Version:
Keywords: migrations | 1.7-beta-1
order_with_respect_to | Resolution:
Has patch: 0 | Triage Stage: Accepted

Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by andrewgodwin):

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


Comment:

Urgh, this is nasty. I'm going to have to find some way of pushing back
all Meta changes along with delayed ForeignKeys (this bug will also occur
for index_together and unique_together among others).

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

Django

unread,
May 7, 2014, 4:04:45 AM5/7/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------
Reporter: Jonas | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: assigned
Severity: Release blocker | Version:
Keywords: migrations | 1.7-beta-1
order_with_respect_to | Resolution:
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by shai):

* cc: shai (added)


Comment:

This is odd. I'd expect all the fields to be created immediately, and only
the constraint creation to be delayed.

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

Django

unread,
May 7, 2014, 12:31:48 PM5/7/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------
Reporter: Jonas | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: assigned
Severity: Release blocker | Version:
Keywords: migrations | 1.7-beta-1
order_with_respect_to | Resolution:
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by andrewgodwin):

Hmm, that might be possible - we could replace the AddField(ForeignKey)
with AddField(IntegerField) (or whatever the underlying type is) and then
have the second thing be the AlterField to a ForeignKey. I'll try that
approach.

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

Django

unread,
May 20, 2014, 8:43:10 AM5/20/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------
Reporter: Jonas | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: assigned
Severity: Release blocker | Version:
Keywords: migrations | 1.7-beta-1
order_with_respect_to | Resolution:
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jezevec):

* cc: jezevec (added)


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

Django

unread,
May 21, 2014, 10:39:17 AM5/21/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------
Reporter: Jonas | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: assigned
Severity: Release blocker | Version:
Keywords: migrations | 1.7-beta-1
order_with_respect_to | Resolution:
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by beathan):

* cc: beathan (added)


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

Django

unread,
Jun 5, 2014, 11:41:43 PM6/5/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------
Reporter: Jonas | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: assigned
Severity: Release blocker | Version:
Keywords: migrations | 1.7-beta-1
order_with_respect_to | Resolution:
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by andrewgodwin):

Just noting that this will be fixed as part of the big autodetector
rewrite.

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

Django

unread,
Jun 6, 2014, 2:37:38 AM6/6/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------
Reporter: Jonas | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: assigned
Severity: Release blocker | Version:
Keywords: migrations | 1.7-beta-1
order_with_respect_to | Resolution:
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by andrewgodwin):

This didn't land in the main autodetector merge, but it can now be fixed
as that's in place. I plan to add a new operation for alterations to
order_with_respect_to (or perhaps just alteration of model options in
general) and implement that with proper dependency to fix this.

--
Ticket URL: <https://code.djangoproject.com/ticket/22470#comment:9>

Django

unread,
Jun 15, 2014, 5:57:30 PM6/15/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------
Reporter: Jonas | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: closed

Severity: Release blocker | Version:
Keywords: migrations | 1.7-beta-1
order_with_respect_to | Resolution: fixed

Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Andrew Godwin <andrew@…>):

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


Comment:

In [changeset:"e5cde2062c0fbe21476978cc318c84134bf7bc46"]:
{{{
#!CommitTicketReference repository=""
revision="e5cde2062c0fbe21476978cc318c84134bf7bc46"
[1.7.x] Fixed #22470: Full migration support for order_with_respect_to
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22470#comment:10>

Django

unread,
Jun 15, 2014, 5:57:32 PM6/15/14
to django-...@googlegroups.com
#22470: Migrations fail with order_with_respect_to and ForeignKey to self
-------------------------------------+-------------------------------------
Reporter: Jonas | Owner:
Type: Bug | andrewgodwin
Component: Migrations | Status: closed
Severity: Release blocker | Version:
Keywords: migrations | 1.7-beta-1
order_with_respect_to | Resolution: fixed
Has patch: 0 | Triage Stage: Accepted
Needs tests: 0 | Needs documentation: 0
Easy pickings: 0 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Andrew Godwin <andrew@…>):

In [changeset:"a8ce5fdc28bdbe091476d49d014d68f8ba4353e7"]:
{{{
#!CommitTicketReference repository=""
revision="a8ce5fdc28bdbe091476d49d014d68f8ba4353e7"


Fixed #22470: Full migration support for order_with_respect_to
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/22470#comment:11>

Reply all
Reply to author
Forward
0 new messages