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
On Oct 30, 11:55 pm, "Michael Graff" <skan.gryp...@gmail.com> wrote:
> 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 youruuidgenerator?
> --Michael
> On 10/30/07, Chief7 Chief7 <rails-mailing-l...@andreas-s.net> wrote:
> > In MySQL, theUUID() function returns a "Universal Unique Identifier".
> > "AUUIDis 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 theUUIDfunction
> > 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.
> > --
> > Posted viahttp://www.ruby-forum.com/