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

A UUID primary key will cause problems not only with generic relations, but with efficiency in general: every foreign key will be significantly more expensive—both to store, and to join on—than a machine word However, nothing requires the UUID to be the primary key: just make it a secondary key, by supplementing your model with a uuid field with unique=True Use the implicit primary key as normal (internal to your system), and use the UUID as your external identifier.

A UUID primary key will cause problems not only with generic relations, but with efficiency in general: every foreign key will be significantly more expensive—both to store, and to join on—than a machine word. However, nothing requires the UUID to be the primary key: just make it a secondary key, by supplementing your model with a uuid field with unique=True. Use the implicit primary key as normal (internal to your system), and use the UUID as your external identifier.

In addition, you can override save and generate your UUID there when an object is being saved the first time (by checking if the object has a primary key). – Joe Holloway Oct 15 '10 at 16:44 1 Joe Holloway, no need for that: you can simply supply the UUID generation function as the field's default. – Piet Delport Oct 15 '10 at 16:54 Thanks Piet.

Your solution is what I'm doing now and it works for obscuring the primary key in the URI (although the comment app still shows it in a hidden field in the "create comment" form). Doesn't give me the advantage of being able to easily create non-colliding database rows on separate servers though. Oh well, I guess I'll learn to re-love the integer primary key.

– mitchf Oct 15 '10 at 17:55 1 Joe: I use django_extensions.db.fields. UUIDField to create my UUIDs in my model. It's simple, I just define my field like this: user_uuid = UUIDField() – mitchf Oct 15 '10 at 17:55.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions