Help on Phoenix adding new nested form, Ecto add nested relation

I have a model Candidate, which might has_many model Address
So I made a nested_form for editing candidate & associated addresses

We used Sentry to catch bug, it keep showing a weird bugs like this

Elixir.RuntimeError
you are attempting to change relation :addresses of Candidates.Candidate but the :on_replace option of
this relation is set to :raise.

By default it is not possible to replace or delete embeds and
associations during cast. Therefore Ecto requires all existing
data to be given on update. Failing to do so results in this
error message.

And it logs the params to be like this, so when I edit candidate I also can add a new address to him/her

{
“_utf8”:Preformatted text “✓”,
“_csrf_token”: “Fj1IGGMYKFNZCjINBw1nEXkeKy0DEAAAZUpYSomxr2e9RT4P7VXypQ==”,
“id”: “801a2392-73c7-4864-a2e2-c0f9b2d89b3c”,
“candidate”: {
“phones”: {
“0”: {
“type”: “mobile”,
“number”: “+3843243543534”,
“id”: “62e353e1-4b8c-422e-bdbd-27a6214f9d98”,
“delete”: “false”
}
},
“nested_model_id”: “”,
“form_partial”: “contact_info”,
“addresses”: {
“1522084910323”: {
“city”: “Odessa”,
“street1”: “ABC”,
“street2”: “”,
“country_id”: “a668d02e-3ca2-4833-b665-f830d23a59bd”,
“state_id”: “f286048c-6678-47fa-9476-9df4f47c7b52”,
“address_type”: “Home”,
“zip_code”: “65123”
}
},
“email”: "abcde@ghi.com"
},
“_method”: “put”
}

The thing is the bug is only catched in Sentry, in production enviroment and I don’t know how it happen, and can not reproduce it on my machine, everything work just fine on my machine or my team’s machine.

We’re stuck at this, anyone have any clue about this problem?`

So what’s the problem is, exactly? Some of your users send fewer addresses than expected by ecto?

Hi, the problem is this error happen sometime in production :frowning: we just can’t reproduce it, and we don’t know what’s wrong to fix

`Elixir.RuntimeError
you are attempting to change relation :addresses of Candidates.Candidate but the :on_replace option of
this relation is set to :raise.

By default it is not possible to replace or delete embeds and
associations during cast. Therefore Ecto requires all existing
data to be given on update. Failing to do so results in this
error message.`

Is it possible in your application that multiple users edit the same candidate?
If so, try this to reproduce:
User A opens form for candidate 1.
User B opens form for candidate 1.
User B adds a new address for candidate 1.
User B saves changes and closes form.
User A make some change for candidate 1 (for example, change email or phone number)
User A saves changes -> error occurs

The error is not weird, it’s very descriptive
Somewhere in your schema, you probably have something like this:
schema “candidate” do
has_many: :addresses, Address
which is equal to:
has_many: :addresses, Address, on_replace: :raise
because :raise is the default.

Hi @nico_amsterdam,
Thank you very much, I have found out the cause of it, it happen because we user click on to “Save” the form, the form take some time to run, and if user did not wait but click “Save” a second time, it would cause the error, like sending double request while the first one did not finished yet. Quite related to your suggestion.

So I fixed by disable the form when click “Save”.

Thx for the update