I had to change the primary key of a django model, and I wanted to create a migration for this. The previous model was using django ids.
The migrations will need to do the following things:
- Create a new
uuid
field/column in the database - Iterate over the existing items in the table, to generate an uuid for them
- Update the eventual references to the model so that it continues to work.
- Change the old primary key to a different type
- Mark the new primary key as such
Create the new field in the model
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
To generate the migrations I did django-admin makemigrations
. The migration looks like this:
Running the tests, I figured out that the URLs will need to be updated as well. I found a regexp for UUIDs
#django, #orm, #migrations - Posté dans la catégorie code