#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 )
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 , 9 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 9 years ago
| Component: | Uncategorized → Migrations |
|---|
comment:4 by , 9 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'),
),
I assume this was fixed by the
RenameModelrefactor in #27310 as we don't rely on related objects anymore (which can be missing whenrelated_nameends with'+').I'm not sure this is worth fixing at this point in the 1.10 release cycle.