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.
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.
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.
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
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.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs