How to check if an index already exists in rails?
Rails provides a straightforward way to check if an index exists using the index_exists?
method.
Syntax
index_exists?(table_name, column_name, **options)
Example
Here’s an example of how to use index_exists?
class AddIndexToUsersEmail < ActiveRecord::Migration[7.1]
def change
unless index_exists?(:users, :email)
add_index(:users, :email)
end
end
end
Following are some more examples of how to use index_exists?
method:
# Checks if an index on multiple columns exists
index_exists?(:users, [:provider, :auth_id])
# Checks if a unique index exists
index_exists?(:users, :email, unique: true)
# Checks if an index with a custom name exists
index_exists?(:users, :email, name: "idx_email")
NOTE: This can also be re-written using the if_not_exists
option in the add_index
method.
class AddIndexToUsersEmail < ActiveRecord::Migration[7.1]
def change
add_index(:users, :email, if_not_exists: true)
end
end