#26404: Change of primary key field does not update foreign keys in migration
--------------------------------+--------------------
     Reporter:  maciej-pawlisz  |      Owner:  nobody
         Type:  Bug             |     Status:  new
    Component:  Migrations      |    Version:  1.9
     Severity:  Normal          |   Keywords:
 Triage Stage:  Unreviewed      |  Has patch:  0
Easy pickings:  0               |      UI/UX:  0
--------------------------------+--------------------
 Changing from:
 {{{
 class ModelA(models.Model):
     new_pk = models.IntegerField()


 class ModelB(models.Model):
     model_a = models.ForeignKey(ModelA)
 }}}

 To:

 {{{
 class ModelA(models.Model):
     new_pk = models.IntegerField(primary_key=True)


 class ModelB(models.Model):
     model_a = models.ForeignKey(ModelA)
 }}}

 Results in migration:
 {{{
 class Migration(migrations.Migration):

     dependencies = [
         ('test_app', '0001_initial'),
     ]

     operations = [
         migrations.RemoveField(
             model_name='modela',
             name='id',
         ),
         migrations.AlterField(
             model_name='modela',
             name='new_pk',
             field=models.IntegerField(primary_key=True, serialize=False),
         ),
     ]
 }}}

 ForeignKey `model_a` in `ModelB` is not altered, so table description at
 least in sqlite is wrong:
 {{{
 CREATE TABLE "test_app_modelb" ("id" integer NOT NULL PRIMARY KEY
 AUTOINCREMENT, "model_a_id" integer NOT NULL REFERENCES "test_app_modela"
 ("id"))
 }}}
 so `connection.check_constraints(['test_app_modelb'])` fails with
 exception
 {{{
 OperationalError: no such column: REFERRED.id
 }}}
 making it impossible to load fixture data.

 Manually adding Operation:
 {{{
         migrations.AlterField(
             model_name='modelb',
             name='model_a',
             field=models.ForeignKey(on_delete=models.deletion.CASCADE,
 to='test_app.ModelA'),
         )
 }}}
 at the end of migration resolves this problem.

--
Ticket URL: <https://code.djangoproject.com/ticket/26404>
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/057.1bf86e7054ad08133b742a276b691224%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to