UUIDs are a popular alternative to auto-incremental integer primary keys.
1 2create_table :users, id: :uuid do |t| 3 t.string :name 4end 5
id: :uuid is passed to
This is all we need to do
to have UUID as primary key for
Now, if an application is designed
to use UUID instead of Integer,
then chances are that
new tables too would use UUID as primary key.
And it can easily get repetitive
every time a new model
Rails 5 comes up with
We need to set primary key as UUID
1 2config.generators do |g| 3 g.orm :active_record, primary_key_type: :uuid 4end 5
This automatically adds
id: :uuid to
in all future migrations.
If we are using the latest version of PostgreSQL then we should enable
pgcrypto extension as per Rails guide.
To enable pgcrypto extension we need a migration which does something like this.
1class EnablePgcryptoExtension < ActiveRecord::Migration 2 def change 3 enable_extension 'pgcrypto' 4 end 5end