How to handle Failing Unique Constraints?

I am wondering how to handle those on a practical level. I have a frontend, where I can import lists of items into my database (prisma + mysql on planetscale). Right now, when a unique constraint fails, the whole import will fail. 1) Is there a way for me to tell my frontend, what item on the list failed the unique constraint? Right now I get:
Invalid `prisma.list.create()` invocation:
Unique constraint failed on the (not available)
at TRPCClientError.from (transformResult-106791
Invalid `prisma.list.create()` invocation:
Unique constraint failed on the (not available)
at TRPCClientError.from (transformResult-106791
2) Is there a way in the backend (i.e. prisma) where I can say that I want it to try to use a certain id, but if that fails (as a backup) generate a new id, instead of failing completely?
3 Replies
bagfaceguy
bagfaceguy10mo ago
Hey man did you find a way to solve this?
Bumble
Bumble10mo ago
You can try/catch and check for this error (check docs for exact error code) and handle as you want. The most common approach is to try to read a record from the db with this id. If it returns something, then generate a new one. If it doesn't, you can safely insert it into db.
bagfaceguy
bagfaceguy10mo ago
Alright man thanks
Want results from more Discord servers?
Add your server