带有外键问题的Rails引擎。

时间:2022-09-20 19:06:27

I'm developing a rails engine and this is my gem.gemspec

我正在开发一个rails引擎,这是我的gemspec


s.required_ruby_version = '>= 2.0.0'

s.add_dependency 'rails', '>= 4.2.0'
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'

This is my migration file, that will be copy to my rails app, when I run the install command:

这是我的迁移文件,当我运行安装命令时,它将被复制到我的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

In my rails app this is my Gemfile

在我的rails应用中,这是我的Gemfile

source 'https://rubygems.org'
# not used yet! source 'https://rails-assets.org'

ruby '2.2.1'

gem 'rails', '4.2.0'

# Front-end
gem 'sass-rails', '~> 5.0'
gem 'coffee-rails'
gem 'uglifier', '>= 1.3.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'bootstrap-sass', '~> 3.3.1.0'
gem 'autoprefixer-rails', '~> 4.0.2.2'

# View
gem 'simple_form', '~> 3.1.0'
gem 'responders', '~> 2.0.2'
gem 'slim-rails', '~> 3.0.1'
gem 'enumerate_it', '~> 1.2.3'

# Infra
gem 'pg', '~> 0.18.1'

# Auth
gem 'devise', '~> 3.4.1'
gem 'devise-i18n-views', '~> 0.3.3'

gem 'mini_magick'
gem 'refile', require: ['refile/rails', 'refile/image_processing']
gem 'aws-sdk', '< 2.0'
gem 'dotenv-rails'

# Association and form helpers
gem 'cocoon', '>= 1.2.6'
gem 'awesome_nested_set'

group :doc do
  gem 'sdoc', require: false
end

group :development, :test do

  # Tests
  gem 'parallel_tests'
  gem 'byebug'
  gem 'spring'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'meta_request'
  gem 'quiet_assets'
  gem 'web-console', '~> 2.0'

  gem 'guard'
  gem 'guard-rspec'
  gem 'guard-bundler'
  gem 'guard-livereload'
  gem 'guard-migrate'

  gem 'foreman'

  gem 'rspec-rails', '~> 3.1.0'
  gem 'teaspoon', '~> 0.8.0'
  gem 'shoulda-matchers', '~> 2.7.0'
  gem 'fuubar', '~> 2.0.0'

  gem 'awesome_print'
  gem 'pry-rails'
end

group :test do
  gem 'capybara', '~> 2.4.4'
  gem 'capybara-screenshot', '~> 1.0.4'
  gem 'poltergeist', '~> 1.5.1'

  gem 'launchy', '~> 2.4.3'
  gem 'database_cleaner', '~> 1.4.0'
  gem 'factory_girl_rails', '~> 4.5.0'
  gem 'forgery', '~> 0.6.0'
  gem 'brazilian_forgery', git: 'https://github.com/internetsistemas/brazilian_forgery.git'

  gem 'simplecov', require: false
  gem 'codeclimate-test-reporter', require: false
end

group :production do
  gem 'rails_12factor'
end

I omitted the paths for my engine, because it's private.

我省略了引擎的路径,因为它是私有的。

Well, here is my problem, with this configs, when I run my migrations RAILS_ENV=test rake db:drop db:create db:migrate and then run my specs rspec everything works well.

这是我的问题,当我运行我的迁移RAILS_ENV=test rake db:drop db:create db: migration然后运行我的spec rspec时,一切都运行得很好。

BUT when I try to update my engine for rails 4.2.1 in my gem.gemspec and Gemfile like this:

但是当我尝试在我的gem中更新rails 4.2.1的引擎时。gemspec和Gemfile如下:

gem.gemspec

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'

Gemfile of my rails app that use my Engine

使用我的引擎的rails应用的Gemfile

gem 'rails', '4.2.1'

rails gem的”、“4.2.1的准备

And then I run bundle update for both, my engine and my rails app.

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

When I run RAILS_ENV=test rake db:drop db:create db:migrate the migrations works well, but when I try to run rspec I got errors related with Foreign Keys, like this:

当我运行RAILS_ENV=test rake db:drop db:创建db:迁移迁移工作正常,但是当我尝试运行rspec时,我就会发现与外键相关的错误,比如:

== 20150416144229 CreateMpaStorySchema: migrating ============================= -- create_table(:mpa_story_archetypes) -> 0.2872s -- create_table(:mpa_story_adjacency_matrices) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:

=== 201504164229 CreateMpaStorySchema: migration ============================= ============================= ======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")

Anyone knows if there are some bugs related with rails 4.2.1 and engines ?

有人知道是否有与rails 4.2.1和引擎相关的bug吗?

1 个解决方案

#1


3  

It's because your migration is generating a prefixed table: mpa_story_archetypes.

这是因为您的迁移生成了一个前缀表:mpa_story_archetypes。

Two possible solutions:

两个可能的解决方案:

1 - Explicitly declare the of your table when you are using the references method...like:

1 -当您使用引用方法时,显式声明您的表。

t.references :mpa_story_archetype, index: true, foreign_key: true

2 - Add the foreign_key by your own, removing the foreign_key option on t.references and calling add_foreign_key method:

2 -自己添加foreign_key,删除t上的foreign_key选项。引用和调用add_foreign_key方法:

add_foreign_key :mpa_story_adjacency_matrices, :mpa_story_archetypes, column: :archetype_id

I prefer the solution 2, and you have to do that in every table which has a foreign_key

我更喜欢解决方案2,你必须在每个有foreign_key的表中都这样做

#1


3  

It's because your migration is generating a prefixed table: mpa_story_archetypes.

这是因为您的迁移生成了一个前缀表:mpa_story_archetypes。

Two possible solutions:

两个可能的解决方案:

1 - Explicitly declare the of your table when you are using the references method...like:

1 -当您使用引用方法时,显式声明您的表。

t.references :mpa_story_archetype, index: true, foreign_key: true

2 - Add the foreign_key by your own, removing the foreign_key option on t.references and calling add_foreign_key method:

2 -自己添加foreign_key,删除t上的foreign_key选项。引用和调用add_foreign_key方法:

add_foreign_key :mpa_story_adjacency_matrices, :mpa_story_archetypes, column: :archetype_id

I prefer the solution 2, and you have to do that in every table which has a foreign_key

我更喜欢解决方案2,你必须在每个有foreign_key的表中都这样做