My schema looks like this:
model Product {
id String @id @default(uuid())
title String @db.VarChar(255)
value Float
description String
quantity Int
companyId String
imgUrl String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Company Company @relation(fields: [companyId], references: [id])
sales Sale[]
ProductsOnSales ProductsOnSales[]
}
model Sale {
id String @id @default(uuid())
client String
company_id String
products Product[]
Company Company @relation(fields: [company_id], references: [id])
ProductsOnSales ProductsOnSales[]
}
model ProductsOnSales {
id String @id @unique @default(uuid())
product_id String
sale_id String
Product Product @relation(fields: [product_id], references: [id])
Sale Sale @relation(fields: [sale_id], references: [id])
@@map("productsOnSales")
}
I created two routes, one to create new sales, and one to get sales, my repository looks like this:
public async getSales(companyId: string): Promise<Sale[]> {
const sales = await this.db.findMany({
where: {
company_id: companyId,
},
include: {
products: true,
},
});
return sales;
}
public async createSale({
client,
companyId,
products,
}: SalesPayload): Promise<void> {
await this.db.create({
data: {
client,
company_id: companyId,
ProductsOnSales: {
createMany: {
data: products.map((product) => ({
product_id: product,
})),
},
},
},
});
}
My problem is, its creating the sales, and relating them on my productsOnSales table, but when I retrieve data from a sale, it does not bring me any products, it just returns an empty array. Could anyone help me?
This is the return i got:
[
{
"id": "b99d1a91-3b50-42a4-9abc-b6f38d11423e",
"client": "Test Company LTDA",
"company_id": "580f9ab9-c52d-4a99-a136-6a87cbae652f",
"products": []
}
]
New contributor
Igor Mazetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.