2

I want to add another index column to my index table,but receive an index on table is too long; the limit is 64 characters. Rails 3 and MySQL DB by the way.

The current schema is as follows:

  create_table "admin_users_projects", :force => true do |t|
    t.integer "admin_user_id"
    t.integer "project_id"
  end

  add_index "admin_users_projects", ["admin_user_id", "project_id"], :name =>     "index_admin_users_projects_on_admin_user_id_and_project_id"

I am trying to run the following migration to add the index:

class AddIndexToAdminUsersProjects < ActiveRecord::Migration
  def change
    add_index :index_admin_users_projects_on_admin_user_id_and_project_id,     :admin_users_project_id
  end
end

But get the following when attempting the rake (in short):

Larrys-MacBook-Pro:scrumtool larrydavid$ rake db:migrate
==  AddIndexToAdminUsersProjects: migrating ===================================
-- add_index(:index_admin_users_projects_on_admin_user_id_and_project_id,     :admin_users_project_id)
rake aborted!
An error has occurred, all later migrations canceled:

Index name     'index_index_admin_users_projects_on_admin_user_id_and_project_id_on_admin_users_project_id'     on table 'index_admin_users_projects_on_admin_user_id_and_project_id' is too long; the limit     is 64 characters
[...]

1 Answer 1

4

Try this

class AddIndexToAdminUsersProjects < ActiveRecord::Migration
  def change
    add_index :admin_users_project, [:admin_users_id, :project_id], :name => 'index_admin_projects_on_admin_and_project'
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly right, you must specify the name yourself. Remember, though, that if you ever want to remove the index through migration afterward, you also have to specify the name there (or, otherwise, create a method that programmatically finds indices based on the columns, and then deletes them).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.