Skip to content
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

Fix Changeset.changed?/2 for assocs #4544

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jesenko
Copy link

@jesenko jesenko commented Nov 5, 2024

It now returns true whenever fetch_change? != :error. Previously, it returned
false when assoc was set to existing struct (as changeset.changes == %{}) and
errored when set to nil (due to nil.changes != %{} check in relation_changed?.

Fixes #4543.

Setting assoc to nil actually errors as it is not handled in changed?/2, while
settin existing assoc to belongs_to assoc returns changed?/2 as false.
It now returns true whenever `fetch_change? != :error`. Previously, it returned
false when assoc was set to existing struct (as `changeset.changes == %{}`) and
errored when set to nil (due to `nil.changes != %{}` check in `relation_changed?`.

Fixes elixir-ecto#4543.
@josevalim
Copy link
Member

We should fix it to handle nil because I think this code is unfortunately wrong. We could have an association that has not changed (i.e. it is an empty changeset).

@jesenko
Copy link
Author

jesenko commented Nov 6, 2024

Hi @josevalim thank you for looking into this. Note that handling nil is not the only issue — there is a valid case where action==:update && changes==%{}, e.g. when setting association to existing struct as in https://github.com/elixir-ecto/ecto/pull/4544/files#diff-8176128515609669c40d0d061249534c49e369abd7bc42c61cdbf5c148da388dR590

@josevalim
Copy link
Member

Yes, we need to check more cases. And potentialy make it recursive. But simply checking if the field is set is not enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ecto.Changeset.changed?/2 returns false when setting assoc and errors when setting assoc to nil.
2 participants