gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:20:12 27 4
gpt4 key购买 nike

我有两个 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 有关 table 。它没有 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com