Opened 7 years ago

Closed 7 years ago

#28639 closed Bug (duplicate)

Changing a primary key doesn't change corresponding foreign keys

Reported by: Chen Chun-Chia Owned by: nobody
Component: Migrations Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Problem Description

Changes of primary key do not apply to all relative models when applying multiple migrations.

Django Version

Both 1.11.1 and 1.11.5 are tested

Reproduce Steps

  1. Initial model
    class Pub(models.Model):
        code = models.CharField(max_length=4, primary_key=True)
    
    
    class Staff(models.Model):
        pub = models.ForeignKey('Pub')
    
    
    class Album(models.Model):
        pub = models.ForeignKey('Pub')
    
    
    class Music(models.Model):
        album = models.ForeignKey('Album')
        name = models.CharField(max_length=32)
    
  1. Make migrations --> 0001_initial.py
  1. Alter max_length of Music.name to 64 --> 0002_alter_music_name.py
  1. Alter max_length of Pub.code to 8 --> 0003_alter_pub_code.py
  1. Final model
    class Pub(models.Model):
        code = models.CharField(max_length=16, primary_key=True)  # 0003_alter_pub_code.py
    
    
    class Staff(models.Model):
        pub = models.ForeignKey('Pub')
    
    
    class Album(models.Model):
        pub = models.ForeignKey('Pub')
    
    
    class Music(models.Model):
        album = models.ForeignKey('Album')
        name = models.CharField(max_length=64)  # 0002_alter_music_name.py
    
  1. Use clean database (drop and recreate database)
  1. Run migrate
  1. Result
    Length of Pub.code : 8
    Length of Staff.pub_id : 4  ### wrong
    Length of Album.pub_id : 8
    

Observation

After applying 0002_alter_music_name.py, only Album exists in Pub._meta.related_objects of state.apps, but Staff is gone.

Change History (2)

comment:1 by Tim Graham, 7 years ago

Summary: Changes of primary key do not apply to all relative models when applying multiple migrationsChanging a primary key doesn't change corresponding foreign keys

Duplicate of #25012.

comment:2 by Tim Graham, 7 years ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top