Skip to content

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

Closed
@jesenko

Description

@jesenko

Elixir version

1.17.2

Database and Version

PostgreSQL 15.8

Ecto Versions

3.12.4

Database Adapter and Versions (postgrex, myxql, etc)

postgrex, latest

Current behavior

   {:ok, existing_profile} = TestRepo.insert(%Profile{name: "profile"})

    changeset =
      %Author{}
      |> Changeset.change()
      |> Changeset.put_assoc(:profile, existing_profile)

    # currently returns false however it is clearly changed
    Changeset.changed?(changeset, :profile)


    changeset =
      %Author{profile: existing_profile}
      |> Changeset.change()
      |> Changeset.put_assoc(:profile, nil)

   # currently crashes 
   Changeset.changed?(changeset, :profile)

Expected behavior

   {:ok, existing_profile} = TestRepo.insert(%Profile{name: "profile"})

    changeset =
      %Author{}
      |> Changeset.change()
      |> Changeset.put_assoc(:profile, existing_profile)

    # should return true
    Changeset.changed?(changeset, :profile) == true


    changeset =
      %Author{profile: existing_profile}
      |> Changeset.change()
      |> Changeset.put_assoc(:profile, nil)

  # should return true
   Changeset.changed?(changeset, :profile) == true

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions