Migrate From One Django Model To Two Models Referenced With A Foreign Key
Solution 1:
I would do a 3-step migration.
- Create the new fields
1.1. Create TypingResult
model and the typing_result = models.ForeignKey(TypingResult, blank=True, null=True)
field. Make sure the FK is optional by making it blank-able and null-able
1.2 Checkpoint by migrating
- Data Migration
2.1 Create an empty migration using this guide https://docs.djangoproject.com/en/1.11/topics/migrations/#data-migrations and add instructions for data migrations.
2.2 The data migration steps are as follows:
Iterate through all
TextResult
for each of them create aTypingResult
with the corresponding dataLink the
TypingResult
to theTextResult
through FK
2.3 Run the migration to checkpoint
- Cleanup
3.1 Delete the wpm and accuracy fields on the TextResult
and make the ForeignKey non-optional.
3.2 Run the migration
Conclusion
This can probably all be done in 1 step, but it's best to understand what is going on. Also adding pdb before a .save()
call will allow you to inspect the steps for the data migration
import pdb; pdb.set_trace()
Post a Comment for "Migrate From One Django Model To Two Models Referenced With A Foreign Key"