@@ -330,9 +330,38 @@ def json(name, options = {})
330330 class TableDefinition < ActiveRecord ::ConnectionAdapters ::TableDefinition
331331 include ColumnMethods
332332
333+ # Defines the primary key field.
334+ # Use of the native PostgreSQL UUID type is supported, and can be used
335+ # by defining your tables as such:
336+ #
337+ # create_table :stuffs, id: :uuid do |t|
338+ # t.string :content
339+ # t.timestamps
340+ # end
341+ #
342+ # By default, this will use the +uuid_generate_v4()+ function from the
343+ # +uuid-ossp+ extension, which MUST be enabled on your databse. To enable
344+ # the +uuid-ossp+ extension, you can use the +enable_extension+ method in your
345+ # migrations To use a UUID primary key without +uuid-ossp+ enabled, you can
346+ # set the +:default+ option to nil:
347+ #
348+ # create_table :stuffs, id: false do |t|
349+ # t.primary_key :id, :uuid, default: nil
350+ # t.uuid :foo_id
351+ # t.timestamps
352+ # end
353+ #
354+ # You may also pass a different UUID generation function from +uuid-ossp+
355+ # or another library.
356+ #
357+ # Note that setting the UUID primary key default value to +nil+
358+ # will require you to assure that you always provide a UUID value
359+ # before saving a record (as primary keys cannot be nil). This might be
360+ # done via the SecureRandom.uuid method and a +before_save+ callback,
361+ # for instance.
333362 def primary_key ( name , type = :primary_key , options = { } )
334363 return super unless type == :uuid
335- options [ :default ] ||= 'uuid_generate_v4()'
364+ options [ :default ] = options . fetch ( :default , 'uuid_generate_v4()' )
336365 options [ :primary_key ] = true
337366 column name , type , options
338367 end
0 commit comments