Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#24447 closed Bug (fixed)

Migrations do not add a FK constraint for an existing column

Reported by: Jean-Louis Fuchs Owned by: nobody
Component: Migrations Version: 1.7
Severity: Normal Keywords:
Cc: Markus Holtermann Triage Stage: Ready for checkin
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Jean-Louis Fuchs)

Pull request: https://github.com/django/django/pull/4235

Consider following model:

class ForeignKeyTest(models.Model):
    id = models.AutoField(primary_key=True)
    customer = models.IntegerField()

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

Then it gets migrated to this:

class ForeignKeyTest(models.Model):
    id = models.AutoField(primary_key=True)
    customer = models.ForeignKey('Customer')

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

The second migration won't create the foreign key. This does not fail with sqlite! Because these migrations are handled differently. It will fail with MySQL and probably Postgres too.

The code didn't detect this case: if old_field.rel doesn't exist alter_field() must always create the foreign key and ONLY if .rel exists it must check .db_constraint, too. Since no .rel also means there was no foreign key before.

Change History (8)

comment:1 Changed 4 years ago by Tim Graham

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

comment:2 Changed 4 years ago by Markus Holtermann

Version: master1.7

comment:3 Changed 4 years ago by Markus Holtermann

Cc: Markus Holtermann added
Needs documentation: set

comment:4 Changed 4 years ago by Jean-Louis Fuchs

Description: modified (diff)
Summary: Migrations do not execute create_fk_sql() when adding a foreign key to a fieldMigrations do not add a FK constraint for an existing column

Removed redundant information and chose clearer title.

comment:5 Changed 4 years ago by Markus Holtermann

Triage Stage: AcceptedReady for checkin

RFC when remaining style update is done

comment:6 Changed 4 years ago by Markus Holtermann <info@…>

Resolution: fixed
Status: newclosed

In f4f0060feaee6bbd76a0d575487682bc541111e4:

Fixed #24447 -- Made migrations add FK constraints for existing columns

When altering from e.g. an IntegerField to a ForeignKey, Django didn't
add a constraint.

comment:7 Changed 4 years ago by Markus Holtermann <info@…>

In 1ae2df6bfc3afadd7edb29a5887711f70cdb52c6:

[1.8.x] Fixed #24447 -- Made migrations add FK constraints for existing columns

When altering from e.g. an IntegerField to a ForeignKey, Django didn't
add a constraint.

Backport of f4f0060feaee6bbd76a0d575487682bc541111e4 from master

comment:8 Changed 4 years ago by Markus Holtermann <info@…>

In 283b630d6324c936cbe42d8f0169f056b3ba59c6:

Fixed #24447 -- Made migrations add FK constraints for existing columns

When altering from e.g. an IntegerField to a ForeignKey, Django didn't
add a constraint.

Backport of f4f0060feaee6bbd76a0d575487682bc541111e4 from master

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