# Intro & Setup

Rails 4 has native support for UUID in PostgreSQL so I decided to give it a try in my test project.

First, you need to enable PostgreSQL extension ‘uuid-ossp’. Create a new migration like this:

And edit the newly created migration file

After that, run rake db:migrate. After this, you can use start using :uuid as your table’s primary key in other migrations. For example:

You can also use :uuid not as ID replacement but on a specific column

# Drawbacks

Using UUID as ID replacement will make Model.first and Model.last methods not working anymore (UUID is all about randomness after all). Luckily, you can use created_at attribute and implement default_scope in your model as following:

Or you can define you own scopes using created_at:

Another problem is that t.references method in your migrations. If your users table have UUID as ID and you define reference to it in other tables using t.references :user, it will create a user_id column with integer as the type in those tables. Of course, it’s not going to work. You must specifically define the reference like this: