-
-
Notifications
You must be signed in to change notification settings - Fork 158
Refactor AttachRelationships: prevent reattachment, support implicit remove #502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This was referenced Jun 7, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem
Consider the following scenario
Person
one-to-onePassport
Person
s items:person1, person2
.Passport
item:passport
PassportId
onperson1
equals id ofpassport
We now do a
PATCH /people/2
and attempt to update the relationship ofperson2
by relating it topassport
. What I would expect it to do is to perform an implicit remove of the old relationship: implicit in the sense that the relationship withperson1
is updated even though it was never explicitly mentioned in the original request.Right now, it results in a 500 because a foreign key constraint is violated. This is because
person1
is already related topassport
and the implicit remove is not taken care of, resulting in a FK constraint error.Update bug reproduced in this test
Issue: a 500 is not acceptable.
Solution
Two options
400 Bad Request
with a body stating that the relation betweenperson1
andpassport
should be removed first, thus more or less reflecting the foreign key constraint error message that is generated by the database~person1
andpassport
before relatingperson2
topassport
. EF Core can handle this trivially if we just attachperson1
to thedbContext
before saving the changes (by just loading the Person relationship of passport). Right now that doesn't happen.I prefer the second option.
[EDIT]
This goes wrong for
CREATE
,UPDATE
for both one-to-one and one-to-many. All goes in a similar way: the entity on the inverse navigation property needs to be attached. This means that the fix is not specific to one particular "pipeline", but theAttachRelationship
method which is called in all involved pipelines needs an upgrade:many-to-many
andone-to-many
: needs to attach relationships of the to-be-manipulated entity so that complete replacement actually works (see Update one-to-many through PATCH on resource not working #492 and Update many-to-many through PATCH on resource not working #494)one-to-one
andone-to-many
: It needs to do attachment of inverse relationships to prevent the error as mentioned above from occurring. See Support implicit remove when creating/updating one-to-one relationship #503: it has a proof of concept for the specific example abovedbContext
elsewhere in the application, seeDefaultEntityRepository.PreventReattachment (private method)
in Update many-to-many through PATCH on resource not working #494.AttachRelationships
is consistent with respect to assigning the actual new relationship values. In the current implementation, forone-to-many
andone-to-one
they are not assigned in this method, but formany-to-many
they are. This is inconsistent, which makes it a lot harder to prevent reattachment in a simple way.The text was updated successfully, but these errors were encountered: