我必须创建一个迁移来进行数据库级别验证。迁移:
class DataBaseLevelValidation < ActiveRecord::Migration
def change
add_index :benefits_business_changes, [:benefit_id, :business_change_id], :unique => true
end
end
我遇到的问题是当我尝试运行rake db:migration
时出现此错误:
Index name 'index_benefits_business_changes_on_benefit_id_and_business_change_id' on table 'benefits_business_changes' is too long;
the limit is 62 characters/Users/mariocardoso/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/schema_statements.rb:797:in `add_index_options'
但是,如果我将名称更改为更短的版本,我会得到:
SQLite3::SQLException: no such table: main.benefits_businessc: CREATE UNIQUE INDEX "index_benefits_businessc_on_benefit_id_and_business_change_id" ON "benefits_businessc"
我该如何克服这个问题?
我看到的唯一方法是将'business_change'模型更改为更短的名称(模型,视图,迁移,......所有内容)。
有没有办法在没有长名称引起错误的情况下运行此迁移?
答案 0 :(得分:8)
你可以做到
add_index :benefits_business_changes, [:benefit_id, :business_change_id], :unique => true, :name => "a_shorter_name"
一个常见的选择是只使用每列的前几个字母。