How to use UUID for Rails API

config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end
rails g migration enable_uuid
class EnableUuid < ActiveRecord::Migration[6.0]end
class EnableUuid < ActiveRecord::Migration[6.0]
def change
enable_extension 'uuid-ossp'
enable_extension 'pgcrypto'
end
end
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users, id: :uuid do |t|
t.string :username
t.timestamps
end
end
end
create_table "devices", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "username"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

IoT is easy … they said

Pair Programming: A Risk Assessment

Transiting from 2019–2020 — A network Engineer’s perspective. — My first Blog

11 Top Tips to Become a Better Software Engineer

The Must Know Checklist For DevOps & Site Reliability Engineers (Updated)

DA Project: Beverage Shop Database Design Work

Scrum Master’s role in Agile Architecture

FAQ: Why Global Percentiles are so important to us as DevOps Engineers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tom Cantwell

Tom Cantwell

More from Medium

Multiprocessing on 3-Dimensional Array

CS 373 Spring 2022: Kyle Kamka

CS373 Spring 2022 : Ramón Márquez — Week 10

CoinEx Smart Chain Multi-Million Dollar Supportive Plan Recruitment.