Need to Replace django default I'd with UUID field

110 views
Skip to first unread message

Rajesh Kumar

unread,
Nov 22, 2022, 1:56:18 PM11/22/22
to django...@googlegroups.com
Hi everyone!
Hope everyone is doing well...

Actually I have 100+ existing data in my database with default I'd field of django 
Now I need to replace that default I'd to UUID.

How I can do  this without loosing any records of my database.

If anyone can give me suggestions that would be great.

Thanks
Rajesh Kumar 

Jason Turner

unread,
Nov 22, 2022, 2:00:22 PM11/22/22
to django...@googlegroups.com
I would just add another column that holds the UUID value instead of changing the default ID. 

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKNDe%3D%3DEa0ZagzpZ-Y_bXKrCi3ZHdG_PNr5%3DgxeJCTbKdM_tdA%40mail.gmail.com.

Rajesh Kumar

unread,
Nov 22, 2022, 2:04:08 PM11/22/22
to django...@googlegroups.com
Hi Jason,
Thanks for a quick reply.

I got your point, but I am worry about my existing data/records which is already associated with id , which is default one.

Sebastian Jung

unread,
Nov 22, 2022, 4:41:08 PM11/22/22
to django...@googlegroups.com
You have also relations like foreignkey to this table? Then you must replace also all ids from foreignkey to new uuid.... Not simple this task

--

Sebastian Jung

unread,
Nov 22, 2022, 4:48:10 PM11/22/22
to django...@googlegroups.com
When you use SQL System like Postgres and you have Version 13+ then you can use gen_random_uuid() to create a uuid. I would make it following: Delete all constraint that checks that id is int and also checks if foreignkey exists in related table.

I would create a Postgres Function with a cursor on your table. Then iterate over table get id. Create new uuid. If relation exists to another tables exists then you must first update relations table with new uuid and then update in original db.

This is my idea but i dont have such a function. This you must write self. But this isn't too much complex.

Good luck

Am Di., 22. Nov. 2022 um 19:55 Uhr schrieb Rajesh Kumar <rjcse...@gmail.com>:
--

Jason

unread,
Nov 22, 2022, 6:25:44 PM11/22/22
to Django users
why?

use uuid for public resource access, and internal int/bigint for primary keys.  that works well with the index structure and doesn't leak sequential information out by URLs.

in other words, a resource is accessed via `somesite.com/some-resource/<a-uuid>

internally, you do a lookup like 

SomeModel.objects.filter(uuid = <request_uuid, related_table__name = "some other thing").all()

Clive Bruton

unread,
Dec 8, 2022, 10:27:12 PM12/8/22
to django...@googlegroups.com
It's more or less the same problem, use a new field to insert your
new foreign keys (UUIDs), looking them up via your old foreign keys
to get the new UUID values. Or, as the other Jason suggests, if you
can, just use the UUIDs for external access.


-- Clive
> d/msgid/django-users/CADoyC17Ar%2B5aHx2GHCdqTuHJagbJx6%
> 3DFzj9zHP19-4-E1j6Pew%40mail.gmail.com.
>
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/
> d/msgid/django-users/CAKNDe%3Dks0Gc%2BR8Hs6u%3DmhtQv3PjwyxaRqF2%
> 3D7pwyP1NF34t5bQ%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages