Skip to content
Browse files

Merge pull request #130 from bolshakov/uuid-primary-key

Explain how to use UUID as primary key instead of integer
  • Loading branch information...
jodosha committed Jan 29, 2016
2 parents ac0ac36 + 9718a4a commit 44831207526806a5b1363197e9e50236aa4c5036
Showing with 25 additions and 0 deletions.
  1. +25 −0 source/guides/models/
@@ -211,3 +211,28 @@ end.load!
<p class="warning">
A custom coercer **MUST** respond to <code>.dump(value)</code> for serialization and to <code>.load(value)</code> for deserialization.

### Use UUID as primary key instead of integer

When using Postgres, you may use [UUID Type]( as primary keys instead if integers.
First of all, you need to enable <code>uuid-ossp<code> extension:

# db/migrations/20160125223305_enable_uuid_extensions.rb
Hanami::Model.migration do
change do
run 'CREATE EXTENSION "uuid-ossp"'

Then, create table with <code>uuid</code> type and set default value:

# db/migrations/20160126223305_create_books.rb
Hanami::Model.migration do
create_table :books do
primary_key :id, :uuid, default: Sequel.function(:uuid_generate_v4)

0 comments on commit 4483120

Please sign in to comment.
You can’t perform that action at this time.