One Advantage of Using UUIDs as Primary Keys

July 07, 2009 at 02:33 PM | Uncategorized | View Comments

A quick note for those unfamiliar with UUIDs or using UUIDs as the primary key of a table in a database:

  • UUID stands for "Universally Unique IDentifier" (they are exactly the same as "GUIDs" (Globally Unique IDentifier), but "universal" sounds cooler than "global"), and are basically† 128 random bits, conveniently grouped into four fields. They look something like this: f81d4fae-7dec-11d0-a765-00a0c91e6bf6. If you're that kind of person, you can read RFC4122 for more information.
  • Because these identifiers are universally unique, they make great primary keys for databases: instead of using a bland auto-increment integer, generate a UUID for each row and use that instead. See linked discussions for more information.

It seems that the question of using UUIDs as PKs has been discussed at length, and I won't try to add to that. I just want to share one useful thing I've been doing with them: giving test data useful names.

Since data are almost always referenced by their primary key, I've found it useful to give my test data "helpful" PKs. For example:

[ { id: "with_children", children: [ ... ] },
  { id: "without_children", children: [ ] } ]

This way it can be easily referenced (for example, http://..../model/with_children) and it's easy to debug (I never need to remember "what was row 13 again?").

It also means I can write test fixtures by hand, without magic numbers everywhere:

- model: models.foo
  id: parent_pk
  fields:
      name: The Parent
      parent: null

- model: models.foo
  id: child_pk
  fields:
      name: The Child
      parent: parent_pk

Nitpickers: yes, this does imply that your UUID implementation can somehow turn ASCII strings into UUIDs.

†: although not entirely – see, for example, the different versions.