Ruby on rails 外键有问题的Rails引擎

Ruby on rails 外键有问题的Rails引擎,ruby-on-rails,rspec,foreign-keys,rails-migrations,rails-engines,Ruby On Rails,Rspec,Foreign Keys,Rails Migrations,Rails Engines,我正在开发一个rails引擎,这是我的gem.gempec s.required_ruby_version = '~> 2.2' s.add_dependency 'rails', '>= 4.2.1' s.add_dependency 'enumerate_it' s.add_dependency 'slim-rails' s.add_dependency 'bootstrap-sass' s.add_dependency 'jquery-rails' s.add_develo

我正在开发一个rails引擎,这是我的
gem.gempec

s.required_ruby_version = '~> 2.2'

s.add_dependency 'rails', '>= 4.2.1'
s.add_dependency 'enumerate_it'
s.add_dependency 'slim-rails'
s.add_dependency 'bootstrap-sass'
s.add_dependency 'jquery-rails'

s.add_development_dependency 'rdoc'
s.add_development_dependency 'tomdoc'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'timecop'
s.add_development_dependency 'shoulda-matchers'
s.add_development_dependency 'factory_girl_rails'
s.add_development_dependency 'database_cleaner'
s.add_development_dependency 'awesome_print'
s.add_development_dependency 'guard'
s.add_development_dependency 'guard-rspec'
s.add_development_dependency 'guard-migrate'
s.add_development_dependency 'guard-livereload'
s.add_development_dependency 'thor'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'codeclimate-test-reporter'

这是我的迁移文件,当我运行install命令时,它将复制到我的rails应用程序:

class CreateMpaStorySchema < ActiveRecord::Migration
  def change
    create_table :mpa_story_archetypes do |t|
      t.string :name
      t.string :api_name
      t.string :api_key
      t.text :description
      t.integer :kind, index: true
      t.integer :tier, index: true
      t.integer :execution, index: true
      t.integer :interaction, index: true
      t.string :klass
      t.integer :expected_runtime

      t.timestamps null: false
    end

    create_table :mpa_story_adjacency_matrices do |t|
      t.references :archetype, index: true, foreign_key: true
      t.integer :kind, index: true
      t.integer :predecessor_id, index: true
      t.integer :successor_id, index: true
      t.integer :branch, default: 0

      t.timestamps null: false
    end

    create_table :mpa_story_instances do |t|
      t.integer :status, index: true
      t.text :artifacts

      t.timestamps null: false
    end

    create_table :mpa_story_instance_steps do |t|
      t.references :archetype, index: true, foreign_key: true
      t.references :instance, index: true, foreign_key: true
      t.integer :kind, index: true
      t.integer :elapsed_time

      t.timestamps null: false
    end

    create_table :mpa_story_instance_artifacts do |t|
      t.references :instance, index: true, foreign_key: true
      t.string :klass, index: true, foreign_key: true
      t.text :value

      t.timestamps null: false
    end

    create_table :mpa_story_protocol_parameters do |t|
      t.references :archetype, index: true, foreign_key: true
      t.integer :interaction, index: true
      t.string :klass
      t.integer :kind, index: true
      t.string :parameter

      t.timestamps null: false
    end

    create_table :mpa_story_archetypes_instances, id: false do |t|
      t.references :archetype, index: true, foreign_key: true
      t.references :instance, index: true, foreign_key: true
    end

    create_table :mpa_story_dynamic_routes do |t|
      t.references :archetype, index: true, foreign_key: true
      t.string :name
      t.text :actions

      t.timestamps null: false
    end

    create_table :mpa_story_dynamic_actions do |t|
      t.references :dynamic_route, index: true, foreign_key: true
      t.integer :kind, index: true
      t.string :name
      t.string :http_method

      t.timestamps null: false
    end

    create_table :mpa_story_business_rules do |t|
      t.references :archetype, index: true, foreign_key: true
      t.string :api_key, index: true

      t.timestamps null: false
    end

    create_table :mpa_story_notifications do |t|
      t.references :archetype, index: true, foreign_key: true
      t.string :api_key, index: true

      t.timestamps null: false
    end

    add_index :mpa_story_archetypes, :api_name, unique: true
    add_index :mpa_story_archetypes, :api_key, unique: true

    create_table :mpa_story_comments do |t|
      t.references :archetype, index: true, foreign_key: true
      t.string :name
      t.text :comment
      t.integer :parent_id, index: true
      t.integer :lft, index: true
      t.integer :rgt, index: true
      t.integer :depth

      t.timestamps null: false
    end
  end
end
我省略了引擎的路径,因为它是私有的

好的,这就是我的问题,当我运行我的迁移
RAILS\u ENV=test rake db:drop db:create db:migrate
然后运行我的specs
rspec
时,我的配置都很好

但是当我尝试在
gem.gempec
Gemfile
中更新rails
4.2.1
引擎时,如下所示:

gem.gemspec

s.required_ruby_version = '~> 2.2'

s.add_dependency 'rails', '>= 4.2.1'
s.add_dependency 'enumerate_it'
s.add_dependency 'slim-rails'
s.add_dependency 'bootstrap-sass'
s.add_dependency 'jquery-rails'

s.add_development_dependency 'rdoc'
s.add_development_dependency 'tomdoc'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'timecop'
s.add_development_dependency 'shoulda-matchers'
s.add_development_dependency 'factory_girl_rails'
s.add_development_dependency 'database_cleaner'
s.add_development_dependency 'awesome_print'
s.add_development_dependency 'guard'
s.add_development_dependency 'guard-rspec'
s.add_development_dependency 'guard-migrate'
s.add_development_dependency 'guard-livereload'
s.add_development_dependency 'thor'
s.add_development_dependency 'simplecov'
s.add_development_dependency 'codeclimate-test-reporter'
使用我的引擎的my rails应用程序的
Gemfile

gem'rails','4.2.1'

然后我为我的引擎和rails应用程序运行
bundle update

当我运行
RAILS\u ENV=test rake db:drop db:create db:migrate
时,迁移工作正常,但当我尝试运行
rspec
时,我遇到了与外键相关的错误,如下所示:

==20150416144229 CreateMpaStorySchema:正在迁移============================= --创建表格(:mpa\U故事\U原型) ->0.2872s --创建表格(:mpa_故事_邻接矩阵) 雷克流产了! StandardError:发生错误,此迁移和所有后续迁移已取消:

PG::UndefinedTable: ERRO:  relação "archetypes" não existe : 
ALTER TABLE    "mpa_story_adjacency_matrices" ADD CONSTRAINT     "fk_rails_2b6894b4f9"
FOREIGN KEY ("archetype_id")
REFERENCES "archetypes" ("id")

有人知道rails 4.2.1和引擎是否存在一些bug吗

这是因为您的迁移正在生成一个前缀表:mpa\u story\u原型

两种可能的解决办法:

1-使用references方法时,显式声明表的

t.references :mpa_story_archetype, index: true, foreign_key: true
2-自行添加外键,删除t.references上的外键选项,并调用Add\u-foreign\u-key方法:

add_foreign_key :mpa_story_adjacency_matrices, :mpa_story_archetypes, column: :archetype_id

我更喜欢解决方案2,并且您必须在每个具有外键的表中执行此操作

您是否确认原型表已经存在?
add_foreign_key :mpa_story_adjacency_matrices, :mpa_story_archetypes, column: :archetype_id