Created
December 31, 2021 13:17
-
-
Save almokhtarbr/875e5500ee0519722eb9e38fb2599e06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a migration to enable uuid# This will generate a new migration file with | |
# timestamp_enable_uuid_extension.rbrails g migration EnableUuidExtension# In migration file add following line to enable pgcryptoclass EnableUuidExtension < ActiveRecord::Migration[6.0] | |
def change | |
enable_extension 'pgcrypto' | |
end | |
end | |
# Create migration for tasks tablerails g migration CreateTasksclass CreateTasks < ActiveRecord::Migration[6.0] | |
def change | |
create_table :tasks, id: :uuid do |t| | |
t.string :title | |
t.datetime :due | |
t.string :status | |
end | |
end | |
end# Here we can see id: :uuid which is essential part to convert uuid as a primary key. | |
# Verify the uuid | |
# Open consolerails c# Create a new task in console. Make sure task.rb is present in | |
# modelTask.create!(title: 'Implement UUID', due: (DateTime.now + 2.hours), status: 'new')#<Task id: "36b4177e-2a82-4ff3-84b6-d6bb33d4103e", title: "Implement UUID", due: '2019-01-01T07:05:30', status: new> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment