Repository navigation
Description
Note: I am only familiar with Postgres, so I will be articulating my problem and suggestions from purely that point of view. It is not my intention that we come up with custom logic for only Postgres. I just don't want to make incorrect assumptions about the other datasources that Prisma supports, so I will not be mentioning them here.
Problem
I support an application that uses Postgres as an underlying DB. Our application makes use of joining tables, with foreign key constraints to keep referential integrity in check.
Let's imagine we're on an online retailer. We sell "products" and we also sell "combos" which represent a collection of products. The schema may be represented as so:
model Combo {
id String @id @default(dbgenerated("uuid_generate_v4()") @db.Uuid
name String @db.VarChar(255)
comboProduct ComboProduct[]
}
model ComboProduct {
comboId String
productId String
upcharge Int
combo Combo @relation(fields: [comboId], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "combo_product_combo_id_foreign")
product Product @relation(fields: [productId], references: [id], onDelete: Cascade, onUpdate: NoAction, map: "combo_product_product_id_foreign")
}
model Product {
id String @id @default(dbgenerated("uuid_generate_v4()") @db.Uuid
name String @db.VarChar(255)
price Int
comboProduct ComboProduct[]
}
Say a user wishes to then create a combo with a set of products in it. Our underlying Prisma query would be:
const createdCombo = await prismaClient.combo.create({
data: {
name,
comboProduct: {
createMany: {
data: comboProductsFromUser
}
}
}
})Imagine there was a non-existent productId supplied within that comboProductsFromUser input by the client. Our API returns the non-existent entry back to the client for convenience. Currently, we catch Prisma's error object and if it failed due to the combo_product_product_id_foreign constraint being trigged, we must query our DB to find which productId doesn't exist, and then proceed to return that to the client.
As you could imagine, this is not only a performance inefficiency, but more importantly, it gets very difficult to go find what's missing in our more complicated API's. It is a drain on development & maintainability of the application.
Suggested solution
Prisma, in some way, returns this invalid entry back when throwing an error response due to a foreign key constraint. I think either could be sufficient in solving the problem, but I'm open to discussion:
- Prisma's error object has a
rawErrorfield, which would be the error message that was thrown by Postgres. We can read that, parse from theDETAILfield of the Postgres response (which contains the invalid entry), and return that invalid ID back to the client. - Prisma's error object is enhanced to contain the failing entry.
Alternatives
We do what we have been doing so far - we lookup non-existent entries within the DB ourselves if an error is thrown.
Additional context
N/A
Activity
s0161026 commentedon May 17, 2023
This feature will be super helpful for our use case so our team won't need to parse the error details to get entity information.
janpio commentedon Jan 30, 2024
Slightly related: #7254