Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#32743 closed Bug (fixed)

Migrations don't alter foreign key data types when referencing primary keys in MTI models.

Reported by: Mariusz Felisiak Owned by: David Wobrock
Component: Migrations Version: 3.2
Severity: Normal Keywords:
Cc: David Wobrock Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (6)

comment:1 by Carlton Gibson, 4 years ago

Triage Stage: UnreviewedAccepted

comment:2 by David Wobrock, 3 years ago

Cc: David Wobrock added
Has patch: set
Owner: changed from nobody to David Wobrock
Status: newassigned

comment:3 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 325d7710:

Fixed #32743 -- Added foreign key altering when altering type of referenced primary key with MTI.

comment:5 by Keryn Knight, 3 years ago

There's an additional PR which been made which alludes to a situation missed by the original fix: https://github.com/django/django/pull/14700
I can't speak for the veracity of it as I haven't been following the details of the ticket, but it's probably worth someone glancing at it and rolling it into this ticket's fix log if appropriate.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 3d9040a5:

Refs #32743 -- Fixed recreation of foreign key constraints when altering type of referenced primary key with MTI.

Follow up to 325d7710ce9f6155bb55610ad6b4580d31263557.

Note: See TracTickets for help on using tickets.
Back to Top