Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a UUID as a primary key in Django models (generic relations impact)

For a number of reasons^, I'd like to use a UUID as a primary key in some of my Django models. If I do so, will I still be able to use outside apps like "contrib.comments", "django-voting" or "django-tagging" which use generic relations via ContentType?

Using "django-voting" as an example, the Vote model looks like this:

class Vote(models.Model):     user         = models.ForeignKey(User)     content_type = models.ForeignKey(ContentType)     object_id    = models.PositiveIntegerField()     object       = generic.GenericForeignKey('content_type', 'object_id')     vote         = models.SmallIntegerField(choices=SCORES) 

This app seems to be assuming that the primary key for the model being voted on is an integer.

The built-in comments app seems to be capable of handling non-integer PKs, though:

class BaseCommentAbstractModel(models.Model):     content_type   = models.ForeignKey(ContentType,             verbose_name=_('content type'),             related_name="content_type_set_for_%(class)s")     object_pk      = models.TextField(_('object ID'))     content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk") 

Is this "integer-PK-assumed" problem a common situation for third-party apps which would make using UUIDs a pain? Or, possibly, am I misreading this situation?

Is there a way to use UUIDs as primary keys in Django without causing too much trouble?


^ Some of the reasons: hiding object counts, preventing url "id crawling", using multiple servers to create non-conflicting objects, ...
like image 241
mitchf Avatar asked Oct 14 '10 18:10

mitchf


People also ask

Should I use UUID as primary key?

Pros. Using UUID for a primary key brings the following advantages: UUID values are unique across tables, databases, and even servers that allow you to merge rows from different databases or distribute databases across servers. UUID values do not expose the information about your data so they are safer to use in a URL.

Should I use UUID Django?

Universally unique identifiers are a good alternative to AutoField for primary_key . The database will not generate the UUID for you, so it is recommended to use default .

Can Django model have two primary keys?

Do Django models support multiple-column primary keys? ¶ No. Only single-column primary keys are supported.

What is the primary key of Django user model?

Django by default makes a primary key field on each model named " id ", with a type of AutoField .


1 Answers

As seen in the documentation, from Django 1.8 there is a built in UUID field. The performance differences when using a UUID vs integer are negligible.

import uuid from django.db import models  class MyUUIDModel(models.Model):     id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) 

You can also check this answer for more information.

like image 68
keithhackbarth Avatar answered Sep 20 '22 10:09

keithhackbarth