Add a UUID Datatype to Your Rails App
Create a migration to add the extension
class CreateUuidExtension < ActiveRecord::Migration
def change
create_table :uuid_extensions do |t|
enable_extension 'uuid-ossp'
end
end
end
Add a column to your table with a data type of :uuid
. Don't forget to add a default of uuid_generate_v4()
class AddUuidToAccounts < ActiveRecord::Migration
def change
add_column :accounts, :uuid, :uuid, default: 'uuid_generate_v4()'
end
end
Tweet