Primary Key

27 views
Skip to first unread message

Soumen Khatua

unread,
Jan 20, 2020, 9:01:59 AM1/20/20
to django...@googlegroups.com
Hi Folks,

I want to generate unique 6 digit primary key for django models and I want to generate this for 10 billon users. So guys please could you tell me How I can do that?

I tried UUID module but it's generating some big numbers.

Thank you

Regards,
Soumen

Forrest Hartley

unread,
Jan 20, 2020, 9:20:35 AM1/20/20
to django...@googlegroups.com
I’m not sure a six digit model would support 10bn unique values. 

--
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/CAPUw6WZSz%3D0sPRJ40VTVHQRD8GtHh3m2zb64Te4ti8RF2f23Vg%40mail.gmail.com.

Kasper Laudrup

unread,
Jan 20, 2020, 9:23:03 AM1/20/20
to django...@googlegroups.com
Hi Soumen,

On 20/01/2020 10.01, Soumen Khatua wrote:
> Hi Folks,
>
> I want to generate unique 6 digit primary key for django models and I
> want to generate this for 10 billon users. So guys please could you tell
> me How I can do that?
>

You want to generate a number between 0 and 1.000.000 that should be
unique for 10.000.000.000 instances?

I hope you can understand why that doesn't make any sense.

Kind regards,

Kasper Laudrup

Kasper Laudrup

unread,
Jan 20, 2020, 9:40:19 AM1/20/20
to django...@googlegroups.com
Hi again,

On 20/01/2020 10.22, Kasper Laudrup wrote:
>
> You want to generate a number between 0 and 1.000.000 that should be
> unique for 10.000.000.000 instances?
>
> I hope you can understand why that doesn't make any sense.
>

Thinking a bit more about, you don't specify whether it should be in
base 10.

If use use base 64 you can represent 68.719.476.736 values which is
enough for representing 10.000.000.00 unique values.

You would still have to generate them so they don't collide with an
existing value (which, unlike a UUID, is bound to happen).

Kind regards,

Kasper Laudrup

Soumen Khatua

unread,
Jan 20, 2020, 10:14:49 AM1/20/20
to django...@googlegroups.com
Thank you for your email

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

Muhammed Rafi A

unread,
Jan 20, 2020, 10:26:39 AM1/20/20
to django...@googlegroups.com
import uuid
uuid.uuid4().hex[:8]

or  


from django.utils.crypto import get_random_string get_random_string(8).lower()

def get_random_string(length=12,
allowed_chars='abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):

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

Kasper Laudrup

unread,
Jan 20, 2020, 11:19:33 AM1/20/20
to django...@googlegroups.com
Hi Muhammed,

On 20/01/2020 11.25, Muhammed Rafi A wrote:
> |importuuid uuid.uuid4().hex[:8]
>
> |
>
> |or
>
>
> |fromdjango.utils.crypto importget_random_string
> get_random_string(8).lower()
>
> ||
>
> def get_random_string(length=12,
>
> allowed_chars='abcdefghijklmnopqrstuvwxyz'
>
> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
>

There's nothing wrong with this as far as I can tell, but I'm fairly
certain this is more or less guaranteed to generate duplicates if
generating 10.000.000.000 values.

Of course, it would be nice to know what OPs use case is. Doesn't sound
like this is the right solution for whatever the problem is he's trying
to solve.

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages