#29854: Altering model primary key might cause referred foreign key attribution
inconsistent
-------------------------------------+-------------------------------------
     Reporter:  Rick Yang            |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Migrations           |                  Version:  1.11
     Severity:  Normal               |               Resolution:
     Keywords:  MySQL, Migration,    |             Triage Stage:
  Altering primary key,              |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Rick Yang:

Old description:

> Django 1.11.8~16 can reproduce this issue.
> DB is MySQL
>
> Reproduced Steps:
> 1. Create models as following:
>
> {{{
> #!python
>
> class TestModel(models.Model):
>     filing_no = models.CharField(max_length=16, primary_key=True)
>

> class OtherModel1(models.Model):
>     id = models.AutoField(primary_key=True)
>
>     # null=True
>     f = models.ForeignKey(TestModel, null=True)
>

> class OtherModel2(models.Model):
>     id = models.AutoField(primary_key=True)
>     f = models.ForeignKey(TestModel)
>

> class OtherModel3(models.Model):
>     id = models.AutoField(primary_key=True)
>     f = models.ForeignKey(TestModel)
>

> class OtherModel4(models.Model):
>     id = models.AutoField(primary_key=True)
>     f = models.ForeignKey(TestModel)
>

> class OtherModel5(models.Model):
>     id = models.AutoField(primary_key=True)
>     f = models.ForeignKey(TestModel)
>

> class OtherModel6(models.Model):
>     id = models.AutoField(primary_key=True)
>     f = models.ForeignKey(TestModel)
>

> class OtherModel7(models.Model):
>     id = models.AutoField(primary_key=True)
>     f = models.ForeignKey(TestModel)
>

> class OtherModel8(models.Model):
>     id = models.AutoField(primary_key=True)
>     f = models.ForeignKey(TestModel)
>

> class OtherModel9(models.Model):
>     id = models.AutoField(primary_key=True)
>     f = models.ForeignKey(TestModel)
>

> class OtherModel10(models.Model):
>     id = models.AutoField(primary_key=True)
>     f = models.ForeignKey(TestModel)
> }}}
>
> 2. Based on above models, we change max_length of TestModel's filing_no
> and auto-generate migration code as following, and then perform
> migrating.
>
> {{{
> #!python
>         migrations.AlterField(
>             model_name='testmodel',
>             name='filing_no',
>             field=models.CharField(max_length=24, primary_key=True,
> serialize=False),
>         ),
> }}}
>
> 3. Check DB schema of table OtherModel1, field f's nullable attribution
> becomes False, and field f's nullable attribution in some other
> OtherModel becomes True.
>

> My investigation:
>
> In function BaseDatabaseSchemaEditor._alter_field(), the function call
> _related_non_m2m_objects(old_field, new_field) will return inconsistent
> "old" and "new" related_objects pairs, and generate wrong SQL command to
> alter foreign keys.

New description:

 Django 1.11.8~16 can reproduce this issue.
 DB is MySQL

 Reproduced Steps:
 1. Create models as following:

 {{{
 #!python

 class TestModel(models.Model):
     filing_no = models.CharField(max_length=16, primary_key=True)


 class OtherModel1(models.Model):
     id = models.AutoField(primary_key=True)

     # null=True
     f = models.ForeignKey(TestModel, null=True)


 class OtherModel2(models.Model):
     id = models.AutoField(primary_key=True)
     f = models.ForeignKey(TestModel)


 class OtherModel3(models.Model):
     id = models.AutoField(primary_key=True)
     f = models.ForeignKey(TestModel)


 class OtherModel4(models.Model):
     id = models.AutoField(primary_key=True)
     f = models.ForeignKey(TestModel)


 class OtherModel5(models.Model):
     id = models.AutoField(primary_key=True)
     f = models.ForeignKey(TestModel)


 class OtherModel6(models.Model):
     id = models.AutoField(primary_key=True)
     f = models.ForeignKey(TestModel)


 class OtherModel7(models.Model):
     id = models.AutoField(primary_key=True)
     f = models.ForeignKey(TestModel)


 class OtherModel8(models.Model):
     id = models.AutoField(primary_key=True)
     f = models.ForeignKey(TestModel)


 class OtherModel9(models.Model):
     id = models.AutoField(primary_key=True)
     f = models.ForeignKey(TestModel)


 class OtherModel10(models.Model):
     id = models.AutoField(primary_key=True)
     f = models.ForeignKey(TestModel)
 }}}

 2. Based on above models, we change max_length of TestModel's filing_no
 and auto-generate migration code as following, and then perform migrating.

 {{{
 #!python
         migrations.AlterField(
             model_name='testmodel',
             name='filing_no',
             field=models.CharField(max_length=24, primary_key=True,
 serialize=False),
         ),
 }}}

 3. Check DB schema of table OtherModel1~10, field f's nullable attribution
 becomes False, and field f's nullable attribution in some other OtherModel
 becomes True.


 My investigation:

 In function BaseDatabaseSchemaEditor._alter_field(), the function call
 _related_non_m2m_objects(old_field, new_field) will return inconsistent
 "old" and "new" related_objects pairs, and generate wrong SQL command to
 alter foreign keys.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29854#comment:2>
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/067.41474b971fecb580b20c34373d4154f1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to