ActiveRecord::StatementInvalid 在两个命名空间模型上

ActiveRecord::StatementInvalid on two namespaced models

我必须在同一个命名空间中使用两个具有 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

到目前为止一切看起来都很棒。正在创建 table 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" =  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_...

到处声明resource_set_idset_id从何而来?我该如何解决这个问题?我想保留两者的命名空间,因为我最终可能会创建项目并设置更多命名空间。

非常感谢,伙计们!

您需要在联接的两边设置外键 table 因为在您的情况下无法推断出它们。

在这种情况下,正确的 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 选项指定连接中的另一个外键 table。