Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporary index name too long in Rails migration

I've got a problem trying to rollback one of my migration. It seems as if Rails is generating a temporary table for the migration, with temporary indices. My actual index on this table is less than 64 characters, but whenever Rails tries to create a temporary index for it, it turns into a name longer than 64 characters, and throws an error.

Here's my simple migration:

class AddColumnNameToPrices < ActiveRecord::Migration
  def self.up
     add_column :prices, :column_name, :decimal
  end

  def self.down
    remove_column :prices, :column_name
  end
end

Here's the error I'm getting:

==  AddColumnNameToPrices: reverting ============================================
-- remove_column(:prices, :column_name)
rake aborted!
An error has occurred, this and all later migrations canceled:

Index name 'temp_index_altered_prices_on_column_and_other_column_and_third_column' on table     'altered_prices' is too long; the limit is 64 characters

I've changed the column names, but the example is still there. I can just make my change in a second migration, but that still means I can't rollback migrations on this table. I can rename the index in a new migration, but that still locks me out of this single migration.

Does anyone have ideas on how to get around this problem?

like image 337
caitlin Avatar asked Dec 27 '22 09:12

caitlin


1 Answers

It looks like your database schema actually has index called prices_on_column_and_other_column_and_third_column. You have probably defined the index in your previous play with migrations. But than just removed index definition from migrations.

If it is true you have 2 options:

  • The simplier one (works if you code is not in production). You can recreate database from scratch using migrations (not from db/schema.rb) by calling rake db:drop db:create db:migrate. Make sure that you do not create this index with long name in other migration files. If you do, add :name => 'short_index_name' options to add_index call to make rails generate shorter name for the index.
  • If you experience this problem on a production database it is a bit more complicated. You might need to manually drop the index from the database console.
like image 123
cryo28 Avatar answered Jan 05 '23 08:01

cryo28