Last active
December 11, 2015 02:49
-
-
Save croby/4533461 to your computer and use it in GitHub Desktop.
Changing a non-primary-key column to a Rails-standard `id` primary key column, and back down again
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
def up | |
rename_column :table_name, :old_id_col, :id | |
change_column :table_name, :id, :primary_key | |
end | |
def down | |
rename_column :table_name, :id, :old_id_col | |
change_column :table_name, :old_id_col, :integer, :null => true | |
remove_column :table_name, :id | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given a table with no primary key set, change the name of the column, then set that column as the primary key. Tricky part here is migrating down, where we have to rename the column to be the old column name, change that column to be an int with
:null => true
, then remove the id column, because for some reason the rename column isn't removing that ID column