I have POST request to save an array of items into a DB. If one of them conflicts with a unique constraint, Is there is a best practice on what to do ?
My first instinct on this case would be to save the unique ones into the DB and just return the duplicate error on the duplicated one. ( In fact this would be better for my use case. )
If that's the way to go, what would be the correct response status ?
Sorry for the conceptual question, but I don't even know how to google this.
CodePudding user response:
The "standard" way of doing things would be to wrap the whole thing in a database transaction and roll back the whole thing. Probably return a 500 error for the return code and a message about the duplicated record.
If you want to support a partial, the return codes are more complicated, you could pick a complicated code like 207 Multi-Status, but support will be limited. Not many developers are going to put in an explicit handler for a 207, so most likely it would be missed on the callback processing. I'd just do a straight 200 and have a response payload with warnings embedded.