wkhatch
wkhatch

Reputation: 2741

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.

Upvotes: 8

Views: 3643

Answers (1)

TomaszSobczak
TomaszSobczak

Reputation: 2926

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

Upvotes: 5

Related Questions