marcamillion
marcamillion

Reputation: 33755

Index name is too long - Rails 3

I am trying to run this migration:

class RemoveClientFromSalesteam < ActiveRecord::Migration
    change_table :sales_teams do |t|
        t.remove :client_id
    end
end

This is the error I am getting:

rake db:migrate
-- change_table(:sales_teams)
rake aborted!
An error has occurred, this and all later migrations canceled:

Index name 'temp_index_altered_sales_teams_on_client_priority_and_personal_priority' on table 'altered_sales_teams' is too long; the limit is 64 characters

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

This is what my schema.rb looks like:

  create_table "sales_teams", :force => true do |t|
    t.string   "name"
    t.integer  "firm_id"
    t.boolean  "client_priority"
    t.boolean  "personal_priority"
    t.datetime "created_at",        :null => false
    t.datetime "updated_at",        :null => false
    t.integer  "client_id"
  end

  add_index "sales_teams", ["client_id"], :name => "index_sales_teams_on_client_id"
  add_index "sales_teams", ["client_priority", "personal_priority"], :name => "index_sales_teams_on_client_priority_and_personal_priority"
  add_index "sales_teams", ["name", "firm_id"], :name => "index_sales_teams_on_name_and_firm_id"

Thoughts?

Thanks.

Upvotes: 4

Views: 2864

Answers (1)

mu is too short
mu is too short

Reputation: 434665

Drop the index, remove your column, and then re-add the index:

def up
  remove_index :sales_teams, :column => [ :client_priority, :personal_priority ]
  remove_column :sales_teams, :client_id
  add_index :sales_teams, [ :client_priority, :personal_priority ]
end

I'm guessing that you're using SQLite, most databases support real ALTER TABLE operations for removing columns but SQLite forces you to copy the table (and indexes), drop the table, and copy everything back; the Rails SQLite driver takes care of this behind the scenes but, apparently, doesn't know about the identifier length limit.

You can also specify your own index names by using the :name option to add_index and remove_index if necessary.

Upvotes: 5

Related Questions