Migrating a custom UUIDField to Django's UUIDField

So I am upgrading a quite old Django application to one of the latest version of Django. And one of the problem I am facing is it uses a third party package, django-uuidfield, to store uuid value in one of the model. But now that library is not maintained and also Django has it’s own UUIDField so I was thinking to upgrade that uuid field and use the Django’s built-in UUIDField. But there are couple of problems that I am facing and would love to get some ideas/hints/guidance on how to tackle those.

# old code
uuid = uuidfield.UUIDField(version=1, hyphenate=True, unique=True, db_index=True)

# new code
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
  • So I uninstalled the old package the application was using which broke the old migration file for that field as it was using that package when the migration was created. Any ideas on what would be the best way to tackle this, currently I commented out the code in migration file which was using that package.

  • The new code created new migrations but I am not able to migrate these migrations as it says uuid column for this model already exists, and also while doing this how do I make sure that the old data of that field is migrated to the new one.

In this case it’s probably better to edit the old migration files to pretend that the field was always a models.UUIDField. If they have the same underlying SQL representation, no migration is required.

Commenting operations out of old migrations is very dangerous as it removes them from the state history and changes will appear with makemigrations or squashing.

1 Like

Thanks @adamchainz, that seems like a very cool trick to solve this problem :slight_smile: