Using UUID/GUID as Primary Key in Rails
Submitted by Paul Dix on Fri, 2005-10-28 09:04.
A project I'm working on has a difficult requirement to meet. It needs to be able to support a multi-master database model. If you're already familiar with the concept, please forgive the following short introduction. This means that multiple systems need to be able to create records in their local database and sync up to other ones later. Connection among the systems is not guaranteed to be up 100% of the time. A prime example of this type of system is Microsoft's Active Directory.
Some of the key concerns when designing this solution are how to select primary keys that will avoid collision and how to keep relational data in tact when syncing data from one database to another. The method I follow for this is to use a UUID or GUID as the primary key for every table, just like Active Directory. When I initially looked at Rails for this project this was a major concern. ActiveRecord ties an auto-incrementing integer as the primary key for all tables/models and I was worried about my ability to override that. Well with a little searching, reading and a tiny amount of effort I was able to get Rails to do exactly what was needed. The following is how I did it.
The first thing you'll need to do is grab UUIDTools, which is a ruby gem written by Bob Aman. The preferred method for installing is through gems from the command line like so:
gem install uuidtools
Then go into your rails application and create uuid_helper.rb in your app/helpers directory. Make that file look like so:
Note: the following code is from this post and its comments:
require 'rubygems' require 'uuidtools' module UUIDHelper def before_create() self.id = UUID.timestamp_create().to_s end end
All that's left in code is to add a line into your models that will use this functionality. The reason it's this incredibly easy is because of Ruby's Mixins (which should be the topic of another post entirely). So if we have a model named Product it would look like this:
class Product < ActiveRecord::Base include UUIDHelper # your module code... end
The last part is the database itself. This will vary depending on what database you use. In my case it's MySQL. Since MySQL doesn't have a UUID field we use VARCHAR(36). I keep the field name the same (id) and everything is ready to go. In the comments of this post (mentioned earlier) Bob suggests the possibility of using a 128 bit integer as an alternative to a 36 character string. At first glance this doesn't seem possible in MySQL. MySQL's BigInt datatype doesn't seem to be able to store the range of values as mentioned in this thread.
So you can see that it's pretty simple to get ActiveRecord using UUIDs for the primary keys. Using Bob's UUIDTools gem we can design a solution that is platform and database independent.
- Paul Dix's blog
- Login or register to post comments









What a great tip! I'm under
What a great tip!
I'm under the impression that your predicament is somewhat taken care of with MySQL 5.0, though I have yet to try out the master/slave stuff in 5.x yet.
Not quite
I'm running MySQL on the primary server, but there are client systems that won't be running it at all. I'm not sure what these will be running, possibly SQLLite or something along those lines. So I have to worry about all the syncing myself without utilizing replication. Also, this mentions that UUID doesn't work with replication but I have to admit to not knowing fully what that means.
I like your post,thank
I like your post,thank you.
tiffany jewelry
tiffany ring
If you want to buy your
If you want to buy your wedding dresses, please come to our wedding dress shop, we have many kinds of wedding gowns. for example, tea length wedding dresses, flower girl dresses, bridesmaid dresses and so on.
Would you want to be one of
Would you want to be one of most beautiful brides in the world? Come to my wedding dresses online, here we offer many kinds of designer wedding dresses, such as alfred angelo wedding dress, mori lee wedding dress, maggie sottero wedding dress and so on. I'm sure you will choose one of your favourite wedding dress.a line lace wedding dresses
Would you want to be one of
I'm sure that every girl wants to be one of most beautiful brides in the world, so a piece of wedding dresses is an important part of them. Our wedding dress online shop has plenty of dresses, many of them are designed by famous designed, such as bonny wedding dresses. Each one is charming and with high quality. Come here and choose your wedding gowns.
gjhjhg
He has also rolled out aion proposals ranging from a higher child-care tax credit to FFXIV GIL a saver's credit in an effort to ease the economic rom gold pain on the middle class. But health care legislation, Obama's central domestic project, is on life support.
Lace Wigs Lace Front
Lace Wigs
Lace Front Wigs
Full Lace Wigs
Custom Lace Wigs
Beyonce Lace Wigs
Indian Lace Wigs
Remy Lace Wigs
Human Hair Wigs
Human Wigs
Straight Wigs
Wavy Wigs
Curly Wigs
Celebrity Wigs
Lace Wigs Lace Front
Lace Wigs
Lace Front Wigs
Full Lace Wigs
Custom Lace Wigs
Beyonce Lace Wigs
Indian Lace Wigs
Remy Lace Wigs
Human Hair Wigs
Human Wigs
Straight Wigs
Wavy Wigs
Curly Wigs
Celebrity Wigs
Columbia Sportswear Columbia
Columbia Sportswear
Columbia Sportswear Jackets
Columbia Jackets
Columbia Clothing
Columbia
Columbia Men's Jackets
Mens Columbia Jackets
Columbia Sportswear Sale
Columbia Sportswear Titanium
Columbia Sportswear Outlet
Columbia Sportswear Clothing
Columbia Sportswear Parka