Hello! Forgive my wording if anything is wrong as I am pretty new to Elixir.
I have transactions and transfers tables. The relation is transactions can have many transfers based on transaction.id (but transactions may also not have any transfers).
How would I go about running an ecto.query to get all the transfers (as an array) for a given transaction.id and add include it as an array in the transaction query?
EDIT 1
Here is my current query, but token_transfers
is only a single item and not an array:
query =
from(
t in Transaction,
inner_join: b in assoc(t, :block),
join: tt in TokenTransfer,
on: tt.transaction_hash == t.hash,
order_by: [{^options.order_by_direction, t.block_number}],
limit: ^options.page_size,
offset: ^offset(options),
select:
merge(map(t, ^@transaction_fields), %{
block_timestamp: b.timestamp,
confirmations: fragment("? - ?", ^max_block_number, t.block_number),
token_transfers: tt,
})
)
EDIT 2
This is what I am trying to achieve. Also, please note that there may not always be a corresponding token_transfer
for every transaction.hash
.
[
transaction: {
hash: 1,
token_transfers: [
{transaction_hash: 1, ...},
{transaction_hash: 1, ...},
{transaction_hash: 1, ...}
]
},
transaction: {
hash: 2,
token_transfers: [
{transaction_hash: 2, ...},
{transaction_hash: 2, ...},
{transaction_hash: 2, ...}
]
},
transaction: {
hash: 3,
token_transfers: []
}
]