I have an update API (PUT/PATCH) entity/{id}
to update a certain entity in my DB. Let's say:
entity {
id: number, // Primary key, unique, not null
content: text,
parentId: number // Foreign key, nullable, points to another entity's Id from the same table
}
When I call this API, if the id
doesn't exist in the DB, it throws an exception and returns 404 Not found
. No problem here.
My question is: what if my id
is valid (i.e. I can update the correct entity), but the payload contains a parentId
of an inexistent entity (so that my FK is invalid)? Should I return 400 Bad Request
, 404 Not Found
or even another 4xx
code?
To be clearer, my requisite is: if the parentId
is invalid, the entity
update should not be made, an error/exception should be thrown and the requester should be informed the request couldn't be fully processed.
I fear using 404 Not Found
could mislead the client to believe the target id is invalid. I also fear that using 400 Bad Request
might imply the client sent a logically invalid payload (e.g. malformatted) instead of a value that came to be "forbidden", which is not the case.
Thanks in advance!
(I know this question is similar to this one, but the answer goes in a different way, so it doesn't help me...)