#29245: ForeignKeyField Renaming leads Remove and AddField instead of 
RenameField
Migration
-------------------------------------+-------------------------------------
               Reporter:  Taqi       |          Owner:  nobody
  Abbas                              |
                   Type:  Bug        |         Status:  new
              Component:             |        Version:  2.0
  Migrations                         |       Keywords:  ForeignKey,
               Severity:  Normal     |  RenameField, db_column
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.e586ac32417d4fa921b4a7c5486ea68c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to