Opened 8 years ago
Closed 8 years ago
#29245 closed Cleanup/optimization (fixed)
ForeignKeyField Renaming leads Remove and AddField instead of RenameField Migration
| Reported by: | Taqi Abbas | Owned by: | nobody |
|---|---|---|---|
| Component: | Migrations | Version: | dev |
| Severity: | Normal | Keywords: | ForeignKey, RenameField, db_column |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Given this field in the model:
class Answer(models.Model): question = models.ForeignKey(question_models.Question, on_delete=models.CASCADE, null=True, blank=True) ...
I changed this to:
class Answer(AbstractModel): _question = models.ForeignKey(question_models.Question, db_column='question_id', on_delete=models.CASCADE, null=True, blank=True) ...
the resulting migration was:
...
migrations.RemoveField(
model_name='answer',
name='question',
),
migrations.AddField(
model_name='answer',
name='_question',
field=models.ForeignKey(blank=True, db_column='question_id', null=True, on_delete=django.db.models.deletion.CASCADE, to='survey.Question'),
),
...
whereas it should have been the following.
...
migrations.RenameField(
model_name='answer',
old_name='question',
new_name='_question',
),
...
But when I manually changed to the above, I get the following error:
ProgrammingError: column survey_answer.question_id does not exist
LINE 1: ...swer"."updated_on", "survey_answer"."is_deleted", "survey_an...
^
HINT: Perhaps you meant to reference the column "survey_answer._question_id".
After this, I ran makemigrations again, without changing any model and this time it made the following:
migrations.AlterField(
model_name='answer',
name='_question',
field=models.ForeignKey(blank=True, db_column='question_id', null=True, on_delete=django.db.models.deletion.CASCADE, to='survey.Question'),
),
The database was PostgreSQL.
Change History (3)
comment:1 by , 8 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|---|
| Type: | Bug → Cleanup/optimization |
| Version: | 2.0 → master |
comment:2 by , 8 years ago
| Has patch: | set |
|---|
Someone at work hit a similar issue so I figured out I'd submit a PR tackling what I suggested above.
Note:
See TracTickets
for help on using tickets.
The issue here is that the auto-detector won't be able to detect field renames if any other options has been changed in the mean time.
Could you confirm that the following works:
db_column='question_id'change.makemigrationsand assert that a migration with anAlterFieldoperation was generated to include thedb_column='question_id'change.questionto_question.makemigrationsand confirm the field rename. Assert that a migration with aRenameFieldoperation was created.At this point both migrations should be SQL noops.
I'm accepting on the basis that the auto-detector rename logic could be enhanced to better handle
db_columnchanges by comparing implicitdb_columnvalues to implicit ones and prompt for a rename.