Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails协会没有';t激发Postgres查询并返回Nil_Ruby On Rails_Postgresql_Foreign Keys_Has Many - Fatal编程技术网

Ruby on rails Rails协会没有';t激发Postgres查询并返回Nil

Ruby on rails Rails协会没有';t激发Postgres查询并返回Nil,ruby-on-rails,postgresql,foreign-keys,has-many,Ruby On Rails,Postgresql,Foreign Keys,Has Many,我有两个与一个模型有许多关联。一个关联工作正常并启动查询,另一个只返回nil。 我正在使用一个只读的远程postgres数据库 以下是模型: #stack_address.rb 类StakeAddress

我有两个
与一个模型有许多关联。一个关联工作正常并启动查询,另一个只返回
nil
。 我正在使用一个只读的远程postgres数据库

以下是模型:

#stack_address.rb
类StakeAddress<应用程序记录
self.table\u name='stack\u address'
有很多:奖励,外键::地址id
有很多:代表团、外键::地址id
有很多:utxo视图、外键::桩号地址\u id
结束
#pool_hash.rb
类PoolHash
下面是我测试关联时发生的情况:

$ rails c
Running via Spring preloader in process 68412
Loading development environment (Rails 6.0.3.4)
2.6.1 :001 > d = Delegation.all.first
  Delegation Load (2.3ms)  SELECT "delegation".* FROM "delegation" ORDER BY "delegation"."id" ASC LIMIT $1  [["LIMIT", 1]]
 => #<Delegation id: 1, addr_id: 61, cert_index: 1, pool_hash_id: 1, active_epoch_no: 210, tx_id: 2424635> 
2.6.1 :002 > d.stake_address
 => nil 
2.6.1 :003 > d.pool_hash
  PoolHash Load (2.3ms)  SELECT "pool_hash".* FROM "pool_hash" WHERE "pool_hash"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
 => #<PoolHash id: 1, hash_raw: "\x158\x06\xDB\xCD\x13M\xDE\xE6\x9A\x8CR\x04\xE3\x8A\xC8\x04H\xF6#B\xF8\xC2<\xFEK~\xDF", view: "pool1z5uqdk7dzdxaae5633fqfcu2eqzy3a3rgtuvy087fdld7..."> 

问题似乎在于您的
委派
表。它没有
stack\u address\u id
列,而是外键驻留在
addr\u id
中。因此,请尝试:

class Delegation < ApplicationRecord
    self.table_name = 'delegation'
    belongs_to :stake_address, foreign_key: "addr_id"
    belongs_to :pool_hash
end
类委托
我明白了,我告诉过
在stack\u address.rb中有很多:代表团、外键::addr\u id
?在协会的双方都需要它?是的,似乎在调用StakeAddress上的关联时使用了该键,如
StakeAddress.delegations
,但在调用
Delegation
模型上的关联时不适用,需要单独定义。
class Delegation < ApplicationRecord
    self.table_name = 'delegation'
    belongs_to :stake_address, foreign_key: "addr_id"
    belongs_to :pool_hash
end