-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Ecto.Changeset.put_assoc() fails when trying to change belongs_to association (sometimes) #1432
Comments
Thank you @carusso! It is all correct. The reason the third does not fail is because you don't have any ID in your structure, so Ecto does not know data is being replaced. The solution is to set :on_replace when you declare the belongs_to and specify what you want to happen. |
Not sure what you mean by "don't have any ID in your structure", since I did have an id on Member: Maybe my confusion was on the naming of that :on_replace option. What exactly is being nilified? Effectively, all I want is for member.club_id to be "replaced". |
I meant FK instead ID. What is being nullified is the FK as well, which is On Sunday, May 15, 2016, Chris R notifications@github.com wrote:
José Valimwww.plataformatec.com.br |
Okay, thank you. I would recommend renaming the :nilify option to something like :force, :accept, or :allow. :nilify implies an end state of nil but doesn't really tell me that the :on_replace is allowing the fk relation to simply be replaced. When I read the documentation looking for replacing the fk, I saw ":nilify" and thought, "that can't be it." In general, the documentation could use a lot more explanation around what is occurring in operations involving associations. When I used similar concepts in Rails, I found it much easier to comprehend what was happening at an abstract level - even though I know that ActiveRecord was doing a bunch of magic behind the scenes that you want to avoid in Phoenix. The flip side of not having the framework do all the magic is that the developer needs a better understanding of the internals. I'll think about it and experiment so I can understand it better and possibly contribute something to the docs. Thanks again. |
Awesome, PRs for docs are always welcome! On Sunday, May 15, 2016, Chris R notifications@github.com wrote:
José Valimwww.plataformatec.com.br |
Even though I've previously read issue elixir-ecto#1432 the knowledge did not stick and I didn't correctly set `on_replace` to `:nilify`. Hopefully this doc update will help.
Even though I've previously read issue #1432 the knowledge did not stick and I didn't correctly set `on_replace` to `:nilify`. Hopefully this doc update will help.
…cto#2350) Even though I've previously read issue elixir-ecto#1432 the knowledge did not stick and I didn't correctly set `on_replace` to `:nilify`. Hopefully this doc update will help.
Elixir: 1.2.4
Postgres: 9.5.2
ecto 2.0.0-rc.5 (Hex package) (mix)
phoenix_ecto 3.0.0-rc.0 (Hex package) (mix)
postgrex 0.11.1 (Hex package) (mix)
OSX 10.11.3
I created a new phoenix project and performed the upgrade to Ecto 2.0.0rc5
Following simple models were created:
So a Member can only belong to one Club and a Club can have many Members
The following test to put_assoc succeeds
The following test fails trying to change the association using actual db model references
Curiously, the following test succeeds by using a struct Member instead
of the actual Member from the insert!().
The error that is returned for that second test case is:
The text was updated successfully, but these errors were encountered: