ActiveRecord的:: StatementInvalid两种型号命名空间

问题描述 投票:0回答:1

我必须在其中拥有的habtm关系相同的命名空间两种车型。

class Resource::Item < ApplicationRecord
    has_and_belongs_to_many :resource_sets, foreign_key: 'resource_item_id', class_name: 'Resource::Set', table_name: 'resource_items_sets'
end

class Resource::Set < ApplicationRecord
    has_and_belongs_to_many :resource_items, foreign_key: 'resource_set_id', class_name: 'Resource::Item', table_name: 'resource_items_sets'
end

迁移已经rails g migration CreateJoinTableResourceItemsResourceSets resource_item resource_set生成

class CreateJoinTableResourceItemsResourceSets < ActiveRecord::Migration[5.2]
    def change
        create_join_table :resource_items, :resource_sets do |t|
            # t.index [:resource_item_id, :resource_set_id]
            # t.index [:resource_set_id, :resource_item_id]
        end
    end
end

到目前为止,一切看起来都很好。该表resource_items_sets正在与两列resource_item_idresource_set_id创建。

这是架构

create_table "resource_items_sets", id: false, force: :cascade do |t|
    t.bigint "resource_item_id", null: false
    t.bigint "resource_set_id", null: false
end

创建resource_item后,我得到被预期以下。

pry(main)> Resource::Item.first.resource_sets
=> #<Resource::Set::ActiveRecord_Associations_CollectionProxy:0x3fdd08748004>

但这样做的以下抛出一个错误。我期待0

pry(main)> Resource::Item.first.resource_sets.count
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column resource_items_sets.set_id does not exist
LINE 1: ...N "resource_items_sets" ON "resource_sets"."id" = "resource_...
                                                         ^
: SELECT "resource_sets"."id" FROM "resource_sets" INNER JOIN "resource_items_sets" ON "resource_sets"."id" = "resource_items_sets"."set_id" WHERE "resource_items_sets"."resource_item_id" = $1 ORDER BY "resource_sets"."name" ASC
from /Users/username/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:677:in `async_prepare'
Caused by PG::UndefinedColumn: ERROR:  column resource_items_sets.set_id does not exist
LINE 1: ...N "resource_items_sets" ON "resource_sets"."id" = "resource_...

哪里set_id来的时候resource_set_id已申报到处?我怎样才能解决这个问题?我想保持其命名空间都因为这样我可能会创建项目和多个命名空间设置。

非常感谢你们!

ruby-on-rails postgresql rails-activerecord has-and-belongs-to-many
1个回答
0
投票

您需要设置在连接表两侧的外键,因为他们不能在你的情况来推断。

在这种情况下,正确的has_and_belongs_to_many调用应该是这样的:

  has_and_belongs_to_many :resource_items,
                          foreign_key: 'resource_set_id',
                          association_foreign_key: 'resource_item_id',
                          class_name: 'Resource::Item',
                          join_table: 'resource_items_sets'
end

class Resource::Item < ApplicationRecord
  has_and_belongs_to_many :resource_sets,
                          foreign_key: 'resource_item_id',
                          association_foreign_key: 'resource_set_id',
                          class_name: 'Resource::Set',
                          join_table: 'resource_items_sets'
end

请注意添加association_foreign_key选项指定连接表中的其它外键。

© www.soinside.com 2019 - 2024. All rights reserved.