Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails5迁移:表没有外键用于_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails5迁移:表没有外键用于

Ruby on rails Rails5迁移:表没有外键用于,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,将Rails 4.2应用程序迁移到Rails 5后,当我尝试从新数据库迁移时,出现以下错误 表“目标”没有发展计划的外键 以下是重要的迁移: 使用add_引用创建外部_键 class AddDevelopmentPlanToObjectives < ActiveRecord::Migration def change add_reference :objectives, :development_plan, index: true, foreign_key: true en

将Rails 4.2应用程序迁移到Rails 5后,当我尝试从新数据库迁移时,出现以下错误

表“目标”没有发展计划的外键

以下是重要的迁移:

  • 使用add_引用创建外部_键

    class AddDevelopmentPlanToObjectives < ActiveRecord::Migration
      def change
        add_reference :objectives, :development_plan, index: true, foreign_key: true
      end
    end
    
    class adddevelopmentplantooobjectives
  • 删除外键(生成错误)

    class DropDevelopmentPlans
  • 迁移在
    remove\u foreign\u key:targets,:development\u plan上中断
    


    有人有这个问题吗?其他类似的迁移也会发生这种情况…

    参考本文:

    Rails 5为迁移类添加了一个版本,因此:

    Rails 5.0中生成的迁移:

    class CreateTasks < ActiveRecord::Migration[5.0]
     ...
    end
    
    class CreateTasks
    如果您没有,它将尝试为我们提供一个兼容层。虽然该兼容层的目标是Rails 4.2,根据该文章,它应该默认为Rails 4.2,但我会尝试将您的迁移更改为all use:

    class AddDevelopmentPlanToObjectives < ActiveRecord::Migration[4.2]
      ....
    
    class adddevelopmentplantooobjectives
    看看这是否有帮助。如果不是,我将更改您的迁移以适应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
    
    class adddevelopmentplantooobjectives
    我认为to_table应该用复数形式:

    remove_foreign_key :objectives, to_table: :development_plans
    

    请参阅。

    那么您的迁移会添加外键,然后一个较晚的迁移会删除它?不管怎样,我刚刚意识到第一个是
    Add
    ,第二个是
    Drop
    remove_foreign_key :objectives, to_table: :development_plans