Switch to UUIDs
Published: November 10, 2020
Updated: November 10, 2020
This is part of The Annotated Guide to a New Rails App, a list of recommendations to make developing your Rails app more productive and joyful.
In this article, we are talking about switching the database primary keys to UUIDs, universally unique identifiers.
On one hand, it is not trivial to change a table’s primary key from the default integer to UUIDs. For that reason, it is helpful to switch to UUIDs as early as possible.
On the other hand, the benefits of using UUIDs are generally not an important concern for new applications, so there is little reason to do it right away.
By default, Rails uses integers as primary keys. Using UUIDs instead prevents ids or the number of entries from being guessed. Using UUIDs also avoids an integer overflow problem if many objects are created.
Enable UUIDs in PostgreSQL:
We assume you have already made the switch to PostgreSQL.
- Generate a migration:
rails generate migration EnableUuids.
- Put this in the change method of the migration:
- Run the migration:
Configure the generators to use UUIDs as primary keys:
config.generators.orm :active_record, primary_key_type: :uuid.
Test the change
After making a commit, test that this works by running
rails generate model foo and
The migration and schema should both indicate that the id is a UUID.
rake db:rollback and remove the generated files before continuing.
When creating a foreign key column (using
belongs_to) you must
indicate the type if the id is a UUID.
create_table :post, id: :uuid do |t| end create_table :comment, id: :uuid do |t| t.belongs_to :post, type: :uuid end
If you do not do this, there is a risk of silent errors when the UUIDs are coerced into an integer to fit into a column that expects an integer.