[英]Rails5 migration : Table has no foreign key for
将我的Rails 4.2应用程序迁移到Rails 5后,当我尝试从新数据库迁移时,出现以下错误
表“目标”没有用于development_plan的外键
以下是重要的迁移:
使用add_reference创建foreign_key
class AddDevelopmentPlanToObjectives < ActiveRecord::Migration def change add_reference :objectives, :development_plan, index: true, foreign_key: true end end
删除外键(生成错误)
class DropDevelopmentPlans < ActiveRecord::Migration def change Objective.all.each do |objective| company = objective.owner.company company.cycles.create name: '4Q 2015', begin_at: Date.today, end_at: 1.year.from_now, current: true unless company.current_cycle company.reload objective.update cycle: company.current_cycle end remove_foreign_key :objectives, :development_plan remove_reference :objectives, :development_plan, index: true drop_table :development_plans end end
迁移在remove_foreign_key :objectives, :development_plan
上中断
有人遇到这个问题吗? 其他类似的迁移也会发生这种情况...
参考本文: https : //blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html
Rails 5为迁移类添加了一个版本,因此:
在Rails 5.0中生成的迁移:
class CreateTasks < ActiveRecord::Migration[5.0]
...
end
如果没有,它将尝试为我们提供兼容性层。 虽然该兼容性层针对的是Rails 4.2,而那是根据该文章的默认值,但我会尝试将您的迁移更改为所有用途:
class AddDevelopmentPlanToObjectives < ActiveRecord::Migration[4.2]
....
看看是否有帮助。 如果没有,我将更改您的迁移以适应v5.0概述的更改
class AddDevelopmentPlanToObjectives < ActiveRecord::Migration[5.0]
def change
add_reference :objectives, :development_plan, foreign_key: true
end
end
class DropDevelopmentPlans < ActiveRecord::Migration[5.0]
def change
Objective.all.each do |objective|
company = objective.owner.company
company.cycles.create name: '4Q 2015', begin_at: Date.today, end_at: 1.year.from_now, current: true unless company.current_cycle
company.reload
objective.update cycle: company.current_cycle
end
remove_foreign_key :objectives, :development_plan
remove_reference :objectives, :development_plan
drop_table :development_plans
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.