Reputation: 752
stackoverflow newbie here, although I've been on here many times to look for helpful answers in the past. This is my first question though, so please be nice :)
I've been trying to figure out how to change a table in an ActiveRecord Migration in Rails by adding (or modifying) the options hash that comes with the create_table command. More specifically, I'm trying to convert a model that uses integer IDs into one that uses UUIDs in Rails 4.0 and postgresql 9.1.9.
So for example, my schema currently looks like this:
create_table :users do |t|
t.string :name
t.timestamps
end
and I want to create a migration to change it to this (following the UUID guide here):
create_table :users, id: false do |t|
t.primary_key :id, :uuid, :default => 'uuid_generate_v1()'
t.string :name
t.timestamps
end
I can add/drop/change columns, but I can't figure out how to put the id: false
into the table. So far the only way I've come up with is to completely nuke the table via drop_table :users
and then re-create it, which seems mighty inefficient to me. Is there some ActiveRecord method I can invoke to add an options hash? Tried to google it with no success, but maybe I don't know what the right search query is?
Any help would be appreciated. Thanks in advance!
Edited: grammar.
Upvotes: 3
Views: 2841
Reputation: 752
Fixed! Discovered that passing in id: false
simply means the id column is not generated in the database, so calling remove_column :users, :id
would drop the column and add id: false
to the schema.
Key insight came from the following older thread: Add Id column in a migration
For completeness, the full migration file:
def up
remove_column :users, :id
add_column :users, :id, :uuid, default: 'uuid_generate_v1()'
end
def down
remove_column :users, :id
add_column :users, :id, :primary_key
end
Upvotes: 5