by Developers 9 Comments

Rails 4 has native support for the type UUID (Universally Unique Identifier) in Postgres. In this Div Bit I will describe how you can use it for generating UUIDs without doing it manually in your Rails code.

First you need to enable the Postgres extension ‘uuid-ossp':


class CreateUuidPsqlExtension < ActiveRecord::Migration
  def self.up
    enable_extension "uuid-ossp"
  end

  def self.down
    disable_extension "uuid-ossp"
  end
end

You can use a UUID as a ID replacement:


create_table :translations, id: :uuid do |t|
  t.string :title
  t.timestamps
end

In this case the Translations table will have a UUID as ID and it is autogenerated. The uuid-ossp extension in Postgresq has different algorithms how the UUID is generated. Rails 4 uses v4 per default. You can read more about these algorithms here: http://www.postgresql.org/docs/current/static/uuid-ossp.html

However, sometimes you don't want to have the UUID as ID replacement and instead have it in a separate column:


class AddUuidToModelsThatNeedIt < ActiveRecord::Migration
  def up
    add_column :translations, :uuid, :uuid
  end

  def down
    remove_column :translations, :uuid
  end
end

This will create a UUID column, but the UUID will not be autogenerated. You have to do it yourself in Rails with SecureRandom. However, we think that this is a typical database responsibility. Fortunately, the default option in add_column helps:


class AddUuidToModelsThatNeedIt < ActiveRecord::Migration
  def up
    add_column :translations, :uuid, :uuid, :default => "uuid_generate_v4()"
  end

  def down
    remove_column :translations, :uuid
  end
end

Now the UUID will be created automatically, also for existing records!

About 

CEO and founder of Lingohub. Envisioning a multilingual digital world. Email me if you have questions about how Lingohub can help you take your products global.

9 Responses to “Dev Bit: Auto generate UUID with Postgres and Rails 4”

  1. Thanks for sharing this!

    The `down` method should remove_column from :translations, not :invoices. :)

    Reply
  2. How about relations? What if you put id: :uuid? Will your belongs_to/has_one(many) relation take this into account?

    Reply
    • Amr N Tamimi

      I made a pull request for belongs_to field for the migrations to read the primary key type and set it for the foreign key. But its rejected. In this case, set the belongs_to field to a limited string.

      Reply
    • Jessie A. Young

      via http://labria.github.io/2013/04/28/rails-4-postgres-uuid-pk-guide/:

      Since UUID primary keys are per-table and not a global setting (there is a plugin in the works to make it global), Rails has no way to tell if the primary key of a table is a integer or a UUID. Because of this,t.references :user will still generate a integer user_id field, which will obviously not work. The solution is simple:

      create_table :posts, id: :uuid do |t|
      t.string :title
      t.uuid :user_id
      t.timestamps
      end

      The same goes for polymorphic relations.

      Reply
  3. You can also use Rails built-in enable_extension, which will do a check to make sure the extension hasn’t already been activated.

    Reply

Leave a Reply

  • (will not be published)