索引迁移名称太长
Name of index migration too long
我必须创建一个迁移来进行数据库级别的验证。迁移:
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' 模型更改为更短的名称(模型、视图、迁移...所有内容)。
有什么方法可以运行这个迁移而不用长名称引起的错误?
你可以做到
add_index :benefits_business_changes, [:benefit_id, :business_change_id], :unique => true, :name => "a_shorter_name"
一个常见的选择是只使用每列的前几个字母。
我必须创建一个迁移来进行数据库级别的验证。迁移:
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' 模型更改为更短的名称(模型、视图、迁移...所有内容)。
有什么方法可以运行这个迁移而不用长名称引起的错误?
你可以做到
add_index :benefits_business_changes, [:benefit_id, :business_change_id], :unique => true, :name => "a_shorter_name"
一个常见的选择是只使用每列的前几个字母。