Skip to content

Instantly share code, notes, and snippets.

@poudelmadhav
Created January 19, 2021 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poudelmadhav/7496cf55b059a081cf6ac7736f97f2bb to your computer and use it in GitHub Desktop.
Save poudelmadhav/7496cf55b059a081cf6ac7736f97f2bb to your computer and use it in GitHub Desktop.
Add unique constrainst on existing foreign_key constrant in rails migration
# Example:
# Adding unique index on user_id(existing foreign_key) of kycs table
class AddUniqueIndexOnUserIdToKycs < ActiveRecord::Migration[6.0]
def change
remove_foreign_key :kycs, :users
remove_index :kycs, :user_id
add_index :kycs, :user_id, unique: true
add_foreign_key :kycs, :users
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment