PostgreSQL UUID primary keys in Rails 5

Published on by   rails, postgres, ruby

In a recent project I have been using UUIDs as the primary key type with Rails 5 and PostgreSQL. This can be useful if your objects IDs are publicly exposed and you want to disguise the fact that they are a sequence, or how early on in the sequence they might be ;-)

How to make it happen…

First you need to enable the uuid-ossp extension which will generate secure v4 UUIDs in the database.

$ bin/rails g migration enable_uuid_extension
class EnableUuidExtension < ActiveRecord::Migration[5.0]
  def change
    enable_extension 'uuid-ossp'

Next up you want to setup a generator so that rails sets all primary key columns to be UUID by default.

# config/initializers/generators.rb

Rails.application.config.generators do |g|
  g.orm :active_record, primary_key_type: :uuid

And that’s all you need to use UUIDs as your primary key. Thanks to Jon McCartie.

Foreign key references

If you get an error trying to run migrations for foreign keys make sure to set the type to :uuid

# example foreign key constraint
t.references :web_page, foreign_key: true, type: :uuid

Adding non primary key UUIDs

You may want to add UUID columns in other places. As long as you’re using uuid_generate_v4() you can use UUIDs as API keys or secure tokens for user lookup. This allows you to have postgres handle generation of these values for you, keeping your Rails app leaner.

class AddSecureTokenToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :secure_token, :uuid, null: false, default: 'uuid_generate_v4()'
    add_index :users, :secure_token, unique: true

I hope this has been useful and if you have anything to add drop a comment below.

Related Articles

It seems like the whole world over is getting ready for GDPR to hit and with 10 days to go if you’re still under pressure here’s something…

Why build a websocket server? When I started building the Void browser extension last year I quickly noticed that adding items from the…

This is another post coming out of working on one of our products, Void. You often see articles teaching testing or TDD on an example topic…

Would you like to work with us?

We would love to hear from you

Get in touch

© Alternate Labs Ltd is a registered company in Northern Ireland NI628282