Skip to content

Commit

Permalink
Adjust behaviour of changed?/2 for assocs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jesenko committed Nov 5, 2024
1 parent 1eaf462 commit 2fce190
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions lib/ecto/changeset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,12 @@ defmodule Ecto.Changeset do
case fetch_change(changeset, field) do
{:ok, new_value} ->
case type do
{tag, relation} when tag in @relations ->
{tag, _relation} when tag in @relations ->
if opts != [] do
raise ArgumentError, "invalid options for #{tag} field"
end

relation_changed?(relation.cardinality, new_value)
true

_ ->
Enum.all?(opts, fn
Expand All @@ -567,14 +567,6 @@ defmodule Ecto.Changeset do
end
end

defp relation_changed?(:one, changeset) do
changeset.action != :update or changeset.changes != %{}
end

defp relation_changed?(:many, changesets) do
Enum.any?(changesets, &relation_changed?(:one, &1))
end

@doc """
Returns the default empty values used by `Ecto.Changeset`.
Expand Down

0 comments on commit 2fce190

Please sign in to comment.