activerecord-cockroachdb-adapter
activerecord-cockroachdb-adapter copied to clipboard
Error when running rails db:rollback that would result in dropping an index
Given this migration:
class CreateFoos < ActiveRecord::Migration[5.2]
def change
create_table :foos do |t|
t.bigint :number
t.references :user
t.timestamps
end
add_index :foos, [:user_id, :number], unique: true
end
end
Steps to reproduce
- run
rails db:migrateand then - run
rails db:rollback
Expected Result
No error messages and the table and the index gone from the database.
Actual Result
The rollback throws a database error, and the index and table are not dropped from the database.
# ...
PG::InternalError: ERROR: index "index_foos_on_user_id_and_number" is in use as unique constraint (use CASCADE if you really want to drop it)
: DROP INDEX "index_foos_on_user_id_and_number"
# ...
workaround
I found a workaround for it, which works for postgresql and cockroachdb, but is no permanent solution. You need to change the migration to look like this, which will yield the expected result on rollback:
class CreateFoos < ActiveRecord::Migration[5.2]
def up
create_table :foos do |t|
t.bigint :number
t.references :user
t.timestamps
end
add_index :foos, [:user_id, :number], unique: true
end
def down
drop_table :foos
end
end
Not sure if this is something the adapter should be concerned with, or if this is something cockroachdb itself should deal different with (ie. like Postgres)?