Check if unique key exists before create

I'm using react-hook-form with zod validation, and I want to check if a unique key exists on the onBlur field event. I can't put it inside Zod validation nor can I rely on the server to respond with this error after submission, because planet-scale never knows what column caused the error. My workaround was to create a trpc route to check if the key exists, call a it's refetch every onBlur event and conditionally use setError to invalidate the field. But this approach is cumbersome and doesn't seem like a good solution. What could I do instead?
2 Replies
Sturlen
Sturlen17mo ago
is there a spesific reason you need to check on blur? most forms would just respond with an error message after submitting, if there is an error on the server
word
wordOP17mo ago
I have to unique constraints, one for a single key and other for two combined. But, when I get the unique constraint error at the create statement, it comes without the key that caused it, and I'd have to give a vague error response to the user. I looked it up and it seems like PLanetScale currently can't figure out a solution for this "not avaiable" column error message. So, as I can't treat it properly on the server, I'm trying it on the client. An dto have the least amount of queries plus the fast response to the user, I opted for the onBlur event. But I'm totally open for other solutions.
Want results from more Discord servers?
Add your server