Search code examples
ruby-on-railsrails-activerecordrails-migrations

which migration do I run to change this line: t.index ["email"], name: "index_users_on_email", unique: true


I'm trying to temporarily remove the uniqueness constraint on emails in my app. In my schema, the users table currently contains this following line - I'd like to change the true at the end:

t.index ["email"], name: "index_users_on_email", unique: true

I'm very unsure about which migration/syntax I need to run in order for the last part to be false. Is it: rails g migration change_column :users, :index_users_on_email, :index, unique: false? or is there a better way?

Any help would be appreciated! I'm scared of messing things up in the db - still a beginner 🙏


Solution

  • I would suggestion just removing the index and then adding one without an uniqueness constraint. After you are done doing what you need to do and want to re-add it, you can drop the index without the uniqueness constraint and re-add one with out

    # first migration
    remove_index :users, column: :index, unique: true
    add_index :users, :index
    

    And whenever you want to add it back

    # second migration
    remove_index :users, column: :index
    add_index :users, :index, unique: true