Opened 8 years ago
Closed 8 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
- 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)
- Make migrations -->
0001_initial.py
- Alter max_length of Music.name to 64 -->
0002_alter_music_name.py
- Alter max_length of Pub.code to 8 -->
0003_alter_pub_code.py
- 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
- Use clean database (drop and recreate database)
- Run migrate
- 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, onlyAlbumexists inPub._meta.related_objects of state.apps, butStaffis gone.
Change History (2)
comment:1 by , 8 years ago
| Summary: | Changes of primary key do not apply to all relative models when applying multiple migrations → Changing a primary key doesn't change corresponding foreign keys |
|---|
comment:2 by , 8 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
Duplicate of #25012.