ruby-on-rails - Rails 协会不会触发 Postgres 查询并返回 Nil

标签 ruby-on-rails postgresql foreign-keys has-many

我与一个模型有两个 has_many 关联。一个关联正常工作并触发查询,另一个关联仅返回nil。 我正在使用只读远程 postgres 数据库。

以下是型号:

# stake_address.rb
class StakeAddress < ApplicationRecord
    self.table_name = 'stake_address'
    has_many :rewards, foreign_key: :addr_id
    has_many :delegations, foreign_key: :addr_id
    has_many :utxo_views, foreign_key: :stake_address_id
end

# pool_hash.rb
class PoolHash < ApplicationRecord
    self.table_name = 'pool_hash'
    has_many :rewards, foreign_key: :pool_id
    has_many :delegations, foreign_key: :pool_hash_id
end

# delegation.rb
class Delegation < ApplicationRecord
    self.table_name = 'delegation'
    belongs_to :stake_address
    belongs_to :pool_hash
end

这是我测试关联时发生的情况:

$ 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..."> 

d.stake_address 应该在 d.pool_hash 发生时触发查询,而不是什么也没有发生,只返回 nil

这是数据库:

cexplorer=# \d+ delegation
                                                    Table "public.delegation"
     Column      |  Type   | Collation | Nullable |                Default                 | Storage | Stats target | Description 
-----------------+---------+-----------+----------+----------------------------------------+---------+--------------+-------------
 id              | bigint  |           | not null | nextval('delegation_id_seq'::regclass) | plain   |              | 
 addr_id         | bigint  |           | not null |                                        | plain   |              | 
 cert_index      | integer |           | not null |                                        | plain   |              | 
 pool_hash_id    | bigint  |           | not null |                                        | plain   |              | 
 active_epoch_no | bigint  |           | not null |                                        | plain   |              | 
 tx_id           | bigint  |           | not null |                                        | plain   |              | 
Indexes:
    "delegation_pkey" PRIMARY KEY, btree (id)
    "unique_delegation" UNIQUE CONSTRAINT, btree (addr_id, pool_hash_id, tx_id)
Foreign-key constraints:
    "delegation_addr_id_fkey" FOREIGN KEY (addr_id) REFERENCES stake_address(id)
    "delegation_pool_hash_id_fkey" FOREIGN KEY (pool_hash_id) REFERENCES pool_hash(id)
    "delegation_tx_id_fkey" FOREIGN KEY (tx_id) REFERENCES tx(id)

cexplorer=# \d+ stake_address
                                                          Table "public.stake_address"
      Column      |       Type        | Collation | Nullable |                  Default                  | Storage  | Stats target | Description 
------------------+-------------------+-----------+----------+-------------------------------------------+----------+--------------+-------------
 id               | bigint            |           | not null | nextval('stake_address_id_seq'::regclass) | plain    |              | 
 hash_raw         | addr29type        |           | not null |                                           | extended |              | 
 view             | character varying |           | not null |                                           | extended |              | 
 registered_tx_id | bigint            |           | not null |                                           | plain    |              | 
Indexes:
    "stake_address_pkey" PRIMARY KEY, btree (id)
    "unique_stake_address" UNIQUE CONSTRAINT, btree (hash_raw)
Foreign-key constraints:
    "stake_address_registered_tx_id_fkey" FOREIGN KEY (registered_tx_id) REFERENCES tx(id)
Referenced by:
    TABLE "delegation" CONSTRAINT "delegation_addr_id_fkey" FOREIGN KEY (addr_id) REFERENCES stake_address(id)
    TABLE "epoch_stake" CONSTRAINT "epoch_stake_addr_id_fkey" FOREIGN KEY (addr_id) REFERENCES stake_address(id)
    TABLE "reserve" CONSTRAINT "reserve_addr_id_fkey" FOREIGN KEY (addr_id) REFERENCES stake_address(id)
    TABLE "reward" CONSTRAINT "reward_addr_id_fkey" FOREIGN KEY (addr_id) REFERENCES stake_address(id)
    TABLE "stake_deregistration" CONSTRAINT "stake_deregistration_addr_id_fkey" FOREIGN KEY (addr_id) REFERENCES stake_address(id)
    TABLE "stake_registration" CONSTRAINT "stake_registration_addr_id_fkey" FOREIGN KEY (addr_id) REFERENCES stake_address(id)
    TABLE "treasury" CONSTRAINT "treasury_addr_id_fkey" FOREIGN KEY (addr_id) REFERENCES stake_address(id)
    TABLE "tx_out" CONSTRAINT "tx_out_stake_address_id_fkey" FOREIGN KEY (stake_address_id) REFERENCES stake_address(id)
    TABLE "withdrawal" CONSTRAINT "withdrawal_addr_id_fkey" FOREIGN KEY (addr_id) REFERENCES stake_address(id)

cexplorer=# \d+ pool_hash
                                                      Table "public.pool_hash"
  Column  |       Type        | Collation | Nullable |                Default                | Storage  | Stats target | Description 
----------+-------------------+-----------+----------+---------------------------------------+----------+--------------+-------------
 id       | bigint            |           | not null | nextval('pool_hash_id_seq'::regclass) | plain    |              | 
 hash_raw | hash28type        |           | not null |                                       | extended |              | 
 view     | character varying |           | not null |                                       | extended |              | 
Indexes:
    "pool_hash_pkey" PRIMARY KEY, btree (id)
    "unique_pool_hash" UNIQUE CONSTRAINT, btree (hash_raw)
Referenced by:
    TABLE "delegation" CONSTRAINT "delegation_pool_hash_id_fkey" FOREIGN KEY (pool_hash_id) REFERENCES pool_hash(id)
    TABLE "epoch_stake" CONSTRAINT "epoch_stake_pool_id_fkey" FOREIGN KEY (pool_id) REFERENCES pool_hash(id)
    TABLE "pool_owner" CONSTRAINT "pool_owner_pool_hash_id_fkey" FOREIGN KEY (pool_hash_id) REFERENCES pool_hash(id)
    TABLE "pool_retire" CONSTRAINT "pool_retire_hash_id_fkey" FOREIGN KEY (hash_id) REFERENCES pool_hash(id)
    TABLE "pool_update" CONSTRAINT "pool_update_hash_id_fkey" FOREIGN KEY (hash_id) REFERENCES pool_hash(id)
    TABLE "reward" CONSTRAINT "reward_pool_id_fkey" FOREIGN KEY (pool_id) REFERENCES pool_hash(id)
    TABLE "slot_leader" CONSTRAINT "slot_leader_pool_hash_id_fkey" FOREIGN KEY (pool_hash_id) REFERENCES pool_hash(id)

最佳答案

问题似乎出在您的Delegation 表上。它没有 stake_address_id 列,而是外键驻留在 addr_id 中。所以尝试一下:

class Delegation < ApplicationRecord
    self.table_name = 'delegation'
    belongs_to :stake_address, foreign_key: "addr_id"
    belongs_to :pool_hash
end

关于ruby-on-rails - Rails 协会不会触发 Postgres 查询并返回 Nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65529253/

相关文章:

node.js - 将对象与 createInstance 关联并手动设置它不起作用

SQL 替代使用 WHERE 作为子查询

android - 房间的可空外键

ruby-on-rails - 渲染 :template and render :partial in rails 3 之间的差异

ruby-on-rails - 您可以使用 Webpacker 直接在 Rails 6 应用程序中使用 Web3.js 吗?

sql - 左连接 - 使用 where (null <> 'p') 时的行为

postgresql - 属性错误 : 'ParseResult' object has no attribute 'drivername'

mysql - 删除表的外键约束错误

ruby-on-rails - rails : how to load 2 models via join?

ruby-on-rails - Rails3 中的 before_create、before_update、before_save、before_destroy 弃用警告