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

339 views
Skip to first unread message

Liuyang Wan

unread,
Nov 10, 2017, 10:56:05 PM11/10/17
to Django users
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


m1chael

unread,
Nov 11, 2017, 9:22:03 AM11/11/17
to django...@googlegroups.com
i would never try to fight my database and django like that. I would add a separate uuid field

--
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...@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.

Liuyang Wan

unread,
Nov 12, 2017, 12:01:55 AM11/12/17
to Django users
Okay, user preference aside. Is this something Django couldn't handle at the moment? If so, it should be submitted as a bug.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Tim Graham

unread,
Nov 12, 2017, 6:32:45 AM11/12/17
to Django users

Liuyang Wan

unread,
Nov 12, 2017, 7:30:45 AM11/12/17
to Django users
Thanks for the link. It’s interesting that two years past the bug is still unresolved.

Thomas Leo

unread,
Feb 19, 2019, 2:59:40 PM2/19/19
to Django users
I'm running django 1.11, have this same issue. Has this issue been fixed in later versions of django? Is there a workaround for 1.11?
Reply all
Reply to author
Forward
0 new messages