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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On 10/29/07, Chief7 Chief7 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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. > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
wentwj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Oct-30 05:05 UTC
Re: Using UUID in a Migration
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 On Oct 29, 9:28 pm, "Brian Hogan" <bpho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > On 10/29/07, Chief7 Chief7 <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > > 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. > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On 10/30/07, Chief7 Chief7 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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. > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
drbuettner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Nov-20 21:21 UTC
Re: Using UUID in a Migration
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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/--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---