#32743: Migrations don't alter foreign key data types when referencing primary 
keys
in MTI models.
--------------------------------------------+------------------------
               Reporter:  Mariusz Felisiak  |          Owner:  nobody
                   Type:  Bug               |         Status:  new
              Component:  Migrations        |        Version:  3.2
               Severity:  Normal            |       Keywords:
           Triage Stage:  Unreviewed        |      Has patch:  0
    Needs documentation:  0                 |    Needs tests:  0
Patch needs improvement:  0                 |  Easy pickings:  0
                  UI/UX:  0                 |
--------------------------------------------+------------------------
 Migrations don't alter foreign key data types when referencing primary
 keys in MTI models, e.g.
 - before state:
 {{{
 class Parent(models.Model):
     pass


 class Child(Parent):
     pass


 class MainModel(models.Model):
     child = models.ForeignKey(Child, models.CASCADE)
 }}}
 - after state:
 {{{
 class Parent(models.Model):
     id = models.BigAutoField(primary_key=True)
 }}}

 `makemigrations` generates a single operation:
 {{{
 migrations.AlterField(
     model_name='parent',
     name='id',
     field=models.BigAutoField(primary_key=True, serialize=False),
 ),
 }}}
 which doesn't change `MainModel.child`'s datatype:
 {{{
 $ python manage.py sqlmigrate test_two 0002
 --
 -- Alter field id on parent
 --
 ALTER TABLE `test_two_child` DROP FOREIGN KEY
 `test_two_child_parent_ptr_id_537e8ba7_fk_test_two_parent_id`;
 ALTER TABLE `test_two_parent` MODIFY `id` bigint AUTO_INCREMENT NOT NULL;
 ALTER TABLE `test_two_child` MODIFY `parent_ptr_id` bigint NOT NULL;
 ALTER TABLE `test_two_child` ADD CONSTRAINT
 `test_two_child_parent_ptr_id_537e8ba7_fk` FOREIGN KEY (`parent_ptr_id`)
 REFERENCES `test_two_parent` (`id`);
 }}}

 and throws an error on MySQL:
 {{{
 MySQLdb._exceptions.OperationalError: (3780, "Referencing column
 'child_id' and referenced column 'parent_ptr_id' in foreign key constraint
 'test_two_mainmodel_child_id_3ca4683f_fk_test_two_' are incompatible.")
 }}}

 I couldn't find an existing ticket.

 Noticed when checking #32742.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32743>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.28eb88b8e02e18ba903321988e9352b2%40djangoproject.com.

Reply via email to