I'm wondering which is the best choice for the HTTP response code in the following situation:
There is a POST / PUT REST API that associates a resource (let's call it source item) with another one (let's call it destination item), using a 1-1 requirement (one-to-one).
If the source item is already associated with another resource, the API should return an error. If the source item is not already associated with any resource, but the destination one is, the API should succeed instead by replacing the current association of the destination.
I'm going with 409 Conflict, but since it indicates a conflict in the target resource, I'm not sure if it is the most appropriate.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409
My interpretation is that the client shouldn't send such request as source item should not be binded to any destination item beforehand.
So the status code is effectively from 4xx family.
Reading the MDN documentation of 409 Conflict does not give a lot of explanations but you might find similarities with your context. The request to bind source and destination entities does conflict with the current state of your source entity (as it's already binded).
To me, your server should repsond 409 Conflict with a detailed explanation on how to solve your problem (unbind source entity of binded destination entity).