On Fri, Jul 19, 2013 at 2:31 AM, Victor Hooi <victorh...@gmail.com> wrote:
> Hi,
>
> I'm just wondering - is it considered good or bad practice to use a Django
> model's in-built ID field?
>
> Say for example you wanted a unique identifier for each transactio - should
> you be generating your own, or can you use just self.id?
>

The problem with using an auto increment field for a unique identifier
is that the values are easily guessable. You are better off adding a
uuid field and either using it as the primary key, or adding it as a
unique key and keeping id as the pk.

The other question is how to store the uuid, which is a 128 bit
number. Most databases don't support such large numbers, and so Django
doesn't provide a UUIDField. You can always store the uuid in 36
(iirc) text characters, but that makes lookups slightly less
efficient. If you use the uuid as the primary key, and you are using a
text field to store it in, then any object that has a foreign key to
this object will also need a 36 character uuid column - ie there is a
cost to having it as the primary key.

Because of this, I usually add a uuid field as a unique key, but leave
id as the primary key.

Cheers

Tom

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to