I know many similar questions have been asked before but I haven't found this question exactly (maybe it's just not possible).
So I have a Column_A in my Rails table (using MySQL). Recently we've had the need to enforce uniqueness on this column.
Is it possible to do a change on this column to make it unique?
The only other solution I came up with is to create a temporary unique column and shuffle everything around. Which would be a pain.
Thanks!
If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again: rename_column :table_name, :old_column1, :new_column1 rename_column :table_name, :old_column2, :new_column2 ...
Go to /db/migrate folder and edit the migration file you made.
Simple two step process:
1: Create a migration
change_column :table_name, :column_name, :string, unique: true
2: Add validation in your model
validates_uniqueness_of :column_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With