Here's how to add a uuid
column to an existing Rails table.
class AddUuidToContacts < ActiveRecord::Migration[5.1]
def change
enable_extension 'uuid-ossp' # => http://theworkaround.com/2015/06/12/using-uuids-in-rails.html#postgresql
add_column :contacts, :uuid, :uuid, default: "uuid_generate_v4()", null: false
execute "ALTER TABLE contacts ADD PRIMARY KEY (uuid);"
end
end
If you forget to add enable_extension 'uuid-ossp'
, you'll get these errors:
PG::UndefinedFunction: ERROR: function uuid_generate_v4() does not
exist ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:
function uuid_generate_v4() does not exist