Search code examples
ruby-on-railspostgresqlforeign-keyshas-many

Rails Association Doesn't Fire Postgres Query and Returns Nil


I have two has_many associations with one model. One association works properly and fires the query, the other one just returns nil. I am working with a read-only remote postgres database.

here are the models:

# 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

and here is what happens when I test the associations:

$ 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 should fire a query as it happens with d.pool_hash instead nothing happens and only nil is returned.

and here is the database:

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)


Solution

  • The issue seems to be with your Delegation table. It does not have a stake_address_id column and instead the foreign key resides in addr_id. So try:

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