#Updating Model of Existing Database

10 messages ยท Page 1 of 1 (latest)

autumn viper
#

Greetings. How does one update a model without the need to delete existing data?

tight cosmos
#

What kind of update do you need to do? Add fields, modify fields? Something else?

#

At a very high level. It's running makemigrations, the migrate

autumn viper
#

I was to add a uuid column to become the new primary key.

#

*want

tight cosmos
#

I wouldn't recommend that (changing the PK to a uuid)
I would recommend adding the uuid field with unique set to True which will achieve a similar thing with less pain.

autumn viper
#

Okay cool. Why do you advise against it?

tight cosmos
#

It means you would need to update everything related model that is linked via a Foreign Key and Many2Many Fields. Not to mention the broad assumption that PKs are integers in various places within Django. Also doing such a migration effectively means you need to create a whole new table, migrate the data, then drop the old one.

Where as adding a field is much simpler migration process.

  1. Add the field with null=True, unique=True
  2. Create an empty migration and add code to populate existing rows with UUIDs
  3. Add a final migration to change null=False to ensure uuids are unique going forward.
autumn viper
#

Ah I see. Many thanks, kind stranger ๐Ÿ˜ƒ

tight cosmos
#

Your welcome ๐Ÿ™‚