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:
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
Hey man did you find a way to solve this?
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.Alright man thanks