I have a table called financial_transaction which have data like this:
| id | debit | credit | financial_type | payment_id | payment | author_id | author |
|---|---|---|---|---|---|---|---|
| 1 | 150 | 0 | Payment | 2 | - | 1 | - |
| 2 | 0 | 50 | Payment | 2 | - | 1 | - |
so my desire from this data is to:
- Group it by
payment_id. - Include true to
author. - Sum all the
debitsandcredits. - Display fields like
financial_typeandpayment_id.
So here is what I want:
[
{
payment_id: 2,
author: {
first_name: John,
last_name: Doe
}
debit: 150,
credit: 50,
financial_type: Payment
}
]
also here's my Prisma Scehma:
enum FinancialType {
Payment
Expense
Withdraw
}
User Table
model User {
id Int @id @default(autoincrement())
first_name String
last_name String
saved_payments Payment[]
saved_financial_transactions FinancialTransaction[]
}
Payment Table
model Payment {
id Int @id @default(autoincrement())
payed_price Decimal
remaining_price Decimal?
price_per_month Decimal
financial_transactions FinancialTransaction[]
author User? @relation(fields: [author_id], references: [id])
author_id Int?
}
FinancialTransaction Table
model FinancialTransaction {
id Int @id @default(autoincrement())
debit Decimal
credit Decimal
financial_type FinancialType
payment Payment @relation(fields: [payment_id], references: [id])
payment_id Int
author User? @relation(fields: [author_id], references: [id])
author_id Int?
}
I tried to get this with the prisma groupBy like in the following:
const financia_transactions = wait prisma.$queryRaw`select jsonb_agg(to_jsonb(t)) from
(
select payment_id,
jsonb_build_object('first_name', u.first_name, 'second_name', u.second_name) author,
sum(debit) debit,
sum(credit) credit,
max(financial_type) financial_type,
from "FinancialTransaction" as ft join "User" u on ft.author_id = u.id
group by payment_id
) t;`;
but this is not working... So my question is there a way Prisma can do the trick and get me this?
