在 Engine I have built 中,我有 3 个类:书籍、类别和作者。每个类的迁移如下。作者和书籍之间以及类别和书籍之间存在一对多关系。引擎将每个类命名为 book_store
。在 Postgresql(而不是 sqlite)上使用相同的迁移时,我收到一个错误,其中一个类不存在 PG::UndefinedTable: ERROR: relation "authors"does not exist
我不确定为什么会出现此错误。命名空间也应该在引用中吗?例如,从这个:
t.references :author, index: true, foreign_key: true
t.references :category, index: true, foreign_key: true
为此:
t.references :book_store_author, index: true, foreign_key: true
t.references :book_store_category, index: true, foreign_key: true
这似乎行不通,因为 BookStore::Books
中会有一个名为 book_store_author
和 book_store_category
的属性导致 BookStore::Books.book_store_author
不会正确地限定在引擎范围内。
我是否需要更改我的迁移代码以在单独的行中反射(reflect)引擎的命名空间?
原始代码
作者
# This migration comes from book_store (originally 20150814153615)
class CreateBookStoreAuthors < ActiveRecord::Migration
def change
create_table :book_store_authors do |t|
t.string :name
t.text :description
t.string :slug
t.timestamps null: false
end
add_index :book_store_authors, :slug, unique: true
end
end
类别
# This migration comes from book_store (originally 20150814153710)
class CreateBookStoreCategories < ActiveRecord::Migration
def change
create_table :book_store_categories do |t|
t.string :title
t.text :description
t.string :slug
t.timestamps null: false
end
add_index :book_store_categories, :slug, unique: true
end
end
书籍
# This migration comes from book_store (originally 20150814153733)
class CreateBookStoreBooks < ActiveRecord::Migration
def change
create_table :book_store_books do |t|
t.string :title
t.string :lead
t.text :excerpt
t.text :description
t.decimal :price
t.integer :cover_type
t.integer :num_pages
t.string :isbn
t.integer :year
t.string :buy_link
t.string :size
t.string :cover_image
t.string :slug
t.references :author, index: true, foreign_key: true
t.references :category, index: true, foreign_key: true
t.timestamps null: false
end
add_index :book_store_books, :slug, unique: true
end
end
错误:
/home/deploy/apps/saturnalia_books/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:797:in `migrate'
/home/deploy/apps/saturnalia_books/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/home/deploy/apps/saturnalia_books/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "authors" does not exist
: ALTER TABLE "book_store_books" ADD CONSTRAINT "fk_rails_52f80cb3c5"
FOREIGN KEY ("author_id")
REFERENCES "authors" ("id")
/home/deploy/apps/saturnalia_books/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/home/deploy/apps/saturnalia_books/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
...
PG::UndefinedTable: ERROR: relation "authors" does not exist
编辑
最佳答案
我通过如下更改模型和迁移文件解决了这个问题。
我有 blog
模型,它通过 tagging
模型有很多 tags
。
所以我更新了 tagging
模型看起来像这样
module Blog class Tagging < ActiveRecord::Base belongs_to :tag, :class_name => "Blog::Tag" belongs_to :post, :class_name => "Blog::Post" end end
然后我像这样修改了 post
模型。
module Blog class Post < ActiveRecord::Base has_many :taggings, :class_name => "Blog::Tagging" has_many :tags, through: :taggings end end
迁移文件修改如下
class CreateBlogTaggings < ActiveRecord::Migration def change create_table :blog_taggings do |t| t.integer :tag_id, index: true, foreign_key: true t.integer :post_id, index: true, foreign_key: true end end
因此,在您的情况下,将 t.references :author
更改为 t.integer :author _id
并将 t.references :category
更改为 t.integer :category_id
.
这应该可以解决您的迁移问题。
关于ruby-on-rails - Rails 4.2 引擎引用列迁移失败 PG::UndefinedTable: ERROR: in postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32301686/