Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27903 closed Bug (fixed)

RenameModel does not not change ForeignKey with related_name='+'

Reported by: Alexander Schrijver Owned by: nobody
Component: Migrations Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alexander Schrijver)

When you create a ForeignKey with a related_name='+' to a second model, and you rename the second model, the model which is referred in the ForeignKey is not updated. I've created a updated OperationTests.test_rename_model test to show this (PR: https://github.com/django/django/pull/8146). This only affects 1.10, this issue seems to be fixed in 1.11.

Change History (4)

comment:1 by Alexander Schrijver, 8 years ago

Description: modified (diff)

comment:2 by Simon Charette, 8 years ago

Component: UncategorizedMigrations

I assume this was fixed by the RenameModel refactor in #27310 as we don't rely on related objects anymore (which can be missing when related_name ends with '+').

I'm not sure this is worth fixing at this point in the 1.10 release cycle.

comment:3 by Tim Graham, 8 years ago

Resolution: fixed
Status: newclosed

I agree with Simon.

comment:4 by Alexander Schrijver, 8 years ago

As a workaround you can do is: before the RenameModel with a AlterField set the ForeignKey's related_name to some temporary value, and then set it back to '+' when you've done the RenameModel. e.g.:

migrations.AlterField(
    model_name='SomeModel',
    name='some_field',
    field=models.ForeignKey(... related_name='xxx', to='app.OtherModel'),
),
migrations.RenameModel('OtherModel', 'OtherModel2'),
migrations.AlterField(
    model_name='SomeModel',
    name='some_field',
    field=models.ForeignKey(... related_name='+', to='app.OtherModel2'),
),
Note: See TracTickets for help on using tickets.
Back to Top