Friday, November 10, 2017

Migration Issues when updating model's default primary key from id to uuid

Let's say in my Django project I have two models A and B.

class A(models.Model):
    something...

class B(models.Model):
    a = models.ForeignKey(A)

Later on I decided to use uuid as the primary key value. so I update the models:

import uuid

class A(models.Model):
     id = models.UUIDField(
         db_index=True,
         default=uuid.uuid4,
         editable=False
    )


class B(models.Model):
     id = models.UUIDField(
         db_index=True,
         default=uuid.uuid4,
         editable=False
    )

    a = models.ForeignKey(A)

Now if I make a migration, Django will NOT pick the changes with the foreign key, i.e the type of fk B.a needs to be updated to uuid as well, not integer anymore.

Is this a bug with current version of Django? What is the best practice for handling such case?

Thanks,
Liuyang


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a68ab6ce-a1c1-42a8-b3d8-82a30edb1079%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment