caitlin
caitlin

Reputation: 161

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?

Upvotes: 4

Views: 3855

Answers (2)

NikoRoberts
NikoRoberts

Reputation: 789

Had this problem today and fixed it by changing the migration to include the dropping and adding of the index causing the long name issue. This way the alteration is not tracked while I am altering the column type (that is where the really long name is caused)

I added the following:

class FixBadColumnTypeInNotifications < ActiveRecord::Migration
  def change
    # replace string ID with integer so it is Postgres friendly
    remove_index :notifications, ["notifiable_id","notifiable_type"]
    change_column :notifications, :notifiable_id, :integer
    # shortened index name
    add_index "notifications", ["notifiable_id","notifiable_type"], :name => "notifs_on_poly_id_and_type"
  end
 end

Upvotes: 3

cryo28
cryo28

Reputation: 1127

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.

Upvotes: 7

Related Questions