2012-10-01 77 views
4

我试图运行这个迁移:指标名称太长 - Rails的3

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

这是我收到的错误:

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) 

这是我的schema.rb样子:

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" 

想法?

谢谢。

回答

5

删除索引,删除您的列,然后重新添加索引:

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 

我猜你正在使用SQLite,大多数数据库支持真正的ALTER TABLE操作除去列,但SQLite的强迫你复制表格(和索引),放下表格,并将所有东西都复制回来; Rails SQLite驱动程序在后台处理这个问题,但显然不知道标识符长度限制。

如果需要,您还可以使用:name选项add_indexremove_index来指定自己的索引名称。

+0

我正在使用SQLite。有趣。 – marcamillion

+0

@marcamillion:SQLite有一定的限制(http://stackoverflow.com/a/8045311/479863),所以当您尝试删除列时,Rails SQLite驱动程序将在您的背后执行复制/删除/复制。看起来司机有一个错误,不知道标识符长度的限制。 –

+0

我试过,但我仍然得到这个错误: ' - add_index(:sales_teams,{:column => [:client_priority,:personal_priority]}) rake aborted! 发生错误,已取消此次及以后的所有迁移: 表'sales_teams'上的索引名'index_sales_teams_on _ {:column => [:client_priority,:personal_priority]}'太长;限制是64个字符。 – marcamillion