Skip to content

Instantly share code, notes, and snippets.

@JamesChevalier
Created November 8, 2016 17:11
Show Gist options
  • Save JamesChevalier/c38dad59d58701618970f1255d97b107 to your computer and use it in GitHub Desktop.
Save JamesChevalier/c38dad59d58701618970f1255d97b107 to your computer and use it in GitHub Desktop.
Simple example of how to get Rails to use UUID as a primary key as well as with a relation

migration to enable uuid in postgres

class EnableUuidExtension < ActiveRecord::Migration[5.0]
  def change
    enable_extension 'uuid-ossp'
  end
end

migration to make the model use uuid as its primary id

class CreateArticles < ActiveRecord::Migration[5.0]
  def change
    create_table :articles, id: :uuid do |t|
      t.timestamps
    end
  end
end

migration to make the relation model use uuid as its id

class CreateComments < ActiveRecord::Migration[5.0]
  def change
    create_table :comments do |t|
      t.timestamps
      t.uuid :article_id
    end

    add_index :comments, :article_id
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment