# Using UUID in a Migration

**URL:** <https://www.ruby-forum.com/t/using-uuid-in-a-migration/118691>\
**Category:** Rails\
**Created:** [October 30, 2007, 3:25am UTC](https://www.ruby-forum.com/t/using-uuid-in-a-migration/118691 "2007-10-30T03:25:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chief7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@chief7](https://www.ruby-forum.com/u/chief7)\
**Post date:** [October 30, 2007, 3:25am UTC](https://www.ruby-forum.com/t/using-uuid-in-a-migration/118691/1 "2007-10-30T03:25:19Z")

</div>

Has anyone ever used UUID in a migration?

I thought I could set the default value of a field to be â€˜UUIDâ€™ but that  
only sets the value the string instead of executing the function to  
create a new unique identifier.

---

<div class="post-metadata">

**Author:** ![chief7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@chief7](https://www.ruby-forum.com/u/chief7)\
**Post date:** [October 30, 2007, 3:29am UTC](https://www.ruby-forum.com/t/using-uuid-in-a-migration/118691/2 "2007-10-30T03:29:20Z")

</div>

Please explain what you mean by UUID. What database?

Rails migrations will create an ID column automatically for you and set  
it  
to your DB’s equivalent of autonumbering.

---

<div class="post-metadata">

**Author:** ![chief7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@chief7](https://www.ruby-forum.com/u/chief7)\
**Post date:** [October 30, 2007, 6:05am UTC](https://www.ruby-forum.com/t/using-uuid-in-a-migration/118691/3 "2007-10-30T06:05:57Z")

</div>

I’m assuming he means a genuine unique identifier. Generally I think  
a hash approximately 32 characters long thats theoretically supposed  
to be unique amongst all tables.

I know what you’re asking about but I haven’t done it in rails yet so  
I’m not much help, though I do recall seeing a plugin that worked with  
and/or generated GUIDs

---

<div class="post-metadata">

**Author:** ![chief7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@chief7](https://www.ruby-forum.com/u/chief7)\
**Post date:** [October 31, 2007, 3:11am UTC](https://www.ruby-forum.com/t/using-uuid-in-a-migration/118691/4 "2007-10-31T03:11:03Z")

</div>

In MySQL, the UUID() function returns a “Universal Unique Identifier”.  
“A UUID is a 128-bit number represented by a string of five hexadecimal  
numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee format.”

There must be a way to set the value of a column using the UUID function  
in a migration.

Has anyone else done this?

Is there a way to set the default value for a column to a db function?  
Such SUM() or RAND()? The syntax should be the same.

---

<div class="post-metadata">

**Author:** ![chief7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@chief7](https://www.ruby-forum.com/u/chief7)\
**Post date:** [October 31, 2007, 6:56am UTC](https://www.ruby-forum.com/t/using-uuid-in-a-migration/118691/5 "2007-10-31T06:56:41Z")

</div>

Not all databases will have that function, so in one go you limit your  
application’s portability.

Why not set it to a string, and write your uuid generator?

–Michael

---

<div class="post-metadata">

**Author:** ![chief7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@chief7](https://www.ruby-forum.com/u/chief7)\
**Post date:** [November 20, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/using-uuid-in-a-migration/118691/6 "2007-11-20T22:22:36Z")

</div>

I’m curious about this as well; I’m currently building an application  
that may use multiple independent databases, and want to have the  
ability to do object replication among them at some point in the  
future for redundancy. Database-generated integer IDs are convenient  
and all but they don’t scale well to this sort of application!

I’ve been designing and testing tables in my Rails app (on MySQL)  
using a UUID for the ‘id’ column, which is declared as a VARCHAR(40)  
with a unique index, and so far I have not encountered any problems.  
Anybody see any real problems with it, conceptually? The problem with  
handling it all within a MySQL model, as previously noted, is that you  
cannot currently assign the value of a function as the default value  
of a column in MySQL, except for a couple of timestamp-type columns.

Here’s what I’ve got in my Rails app:

in lib/uuid.rb:  
class UUID  
def self.new  
return ActiveRecord::Base.connection.select\_value(“SELECT  
LOWER( UUID() )”)  
end  
end

And then in each model:  
def before\_create  
if self.id.nil?  
self.id = UUID.new  
end  
end

And finally, in my table creation migrations:  
def self.up  
create\_table :hot\_folders, :id =\> false do |t|  
t.column :id, :string, :limit =\> 40, :null =\> false  
…  
end  
add\_index(‘hot\_folders’, ‘id’, ‘UNIQUE’)  
end

If you were on a different database engine, or if I am someday, you  
could update the uuid.rb lib file to do something different and  
appropriate for the database you’re using - or generate a UUID  
programatically instead of via the db - via a series of IF/ELSIFs.  
(IF mysql ELSIF sqlserver ELSIF postgres ELSE …)

The above table creation will be somewhat inefficient for MySQL’s  
InnoDB engine, as ISTR that if you don’t declare a unique ID during  
table creation, it creates one internally. When I get to load testing  
& whatnot that may mean dropping and recreating the tables to avoid  
the extra overhead of two unique columns, one of which I never use.

-Dan
