Removing a database unique index constraint in postgres Rails

878 views Asked by At

In my Rails structure.sql, I have this line of code:

CREATE UNIQUE INDEX primary_active_fruit_in_season_constraint ON trees USING btree (user_id) WHERE (is_ripe AND is_in_season);

I no longer need this validation so I am going to remove the validation in the Rails code on the Tree model. But how to I remove this database validation? Where did it come from? What line of code in a rails migration created this? How do I remove it?

1

There are 1 answers

2
mu is too short On

We have no way of knowing who created that index but the WHERE clause attached to it suggests (since AR generally doesn't know about such advanced things) that it was created manually with something like:

connection.execute('create unique index ...')

and you can drop the index in a migration the same way:

def up
  connection.execute('drop index primary_active_fruit_in_season')
end
def down
  connection.execute('CREATE UNIQUE INDEX primary_active_fruit_in_season_constraint ON trees USING btree (user_id) WHERE (is_ripe AND is_in_season)')
end

Manually create a migration file with those two methods in it and rake db:migrate as usual.