Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multi column distinct index using mysql with rails migration

Trying to execute this statement in a rails migration to generate a multi column unique constraint/index:

add_index :contributors, [:project_id, :user_id], :unique=>true

I've also tried providing an optional :name to the method, but still getting the failure. There are no existing keys in this table

Mysql::Error: Duplicate entry '5-9' for key 'distinct_user_and_project': CREATE UNIQUE INDEX `distinct_user_and_project` ON `contributors` (`project_id`, `user_id`)

How do I create this short of using execute and straight sql? Thanks.

like image 721
wkhatch Avatar asked Nov 16 '10 20:11

wkhatch


1 Answers

Its clearly in error (Duplicate entry '5-9') that your data is not unique so You can't add such index there

like image 133
TomaszSobczak Avatar answered Sep 24 '22 00:09

TomaszSobczak