Skip to content

Pretty print given struct on relation error#4611

Merged
wojtekmach merged 1 commit intomasterfrom
wm-pretty-print
May 29, 2025
Merged

Pretty print given struct on relation error#4611
wojtekmach merged 1 commit intomasterfrom
wm-pretty-print

Conversation

@wojtekmach
Copy link
Member

Before:

** (RuntimeError) you have set that the relation :draft of Post
has `:on_replace` set to `:update` but you are giving it a struct/
changeset to put_assoc/put_change.

Since you have set `:on_replace` to `:update`, you are only allowed
to update the existing entry by giving updated fields as a map or
keyword list or set it to nil.

If you indeed want to replace the existing :draft, you have
to change the foreign key field directly.

Got: %PageContent{__meta__: #Ecto.Schema.Metadata<:built, "page_contents">, id: nil, really_long_field1: nil, really_long_field2: nil}

    (ecto 3.13.0-dev) lib/ecto/changeset/relation.ex:279: Ecto.Changeset.Relation.raise_if_updating_with_struct!/2
    (ecto 3.13.0-dev) lib/ecto/changeset/relation.ex:333: Ecto.Changeset.Relation.single_change/6
    (ecto 3.13.0-dev) lib/ecto/changeset/relation.ex:173: Ecto.Changeset.Relation.change/3
    (ecto 3.13.0-dev) lib/ecto/changeset.ex:1920: Ecto.Changeset.put_change/7
    (ecto 3.13.0-dev) lib/ecto/changeset.ex:2168: Ecto.Changeset.put_relation/5
    ecto_sql.exs:62: Main.main/0
    ecto_sql.exs:67: (file)

After:

** (RuntimeError) you have set that the relation :draft of Post
has `:on_replace` set to `:update` but you are giving it a struct/
changeset to put_assoc/put_change.

Since you have set `:on_replace` to `:update`, you are only allowed
to update the existing entry by giving updated fields as a map or
keyword list or set it to nil.

If you indeed want to replace the existing :draft, you have
to change the foreign key field directly.

Got:

%PageContent{
  __meta__: #Ecto.Schema.Metadata<:built, "page_contents">,
  id: nil,
  really_long_field1: nil,
  really_long_field2: nil
}

    (ecto 3.13.0-dev) lib/ecto/changeset/relation.ex:279: Ecto.Changeset.Relation.raise_if_updating_with_struct!/2
    (ecto 3.13.0-dev) lib/ecto/changeset/relation.ex:335: Ecto.Changeset.Relation.single_change/6
    (ecto 3.13.0-dev) lib/ecto/changeset/relation.ex:173: Ecto.Changeset.Relation.change/3
    (ecto 3.13.0-dev) lib/ecto/changeset.ex:1920: Ecto.Changeset.put_change/7
    (ecto 3.13.0-dev) lib/ecto/changeset.ex:2168: Ecto.Changeset.put_relation/5
    ecto_sql.exs:62: Main.main/0
    ecto_sql.exs:67: (file)

@wojtekmach wojtekmach merged commit 44a46fb into master May 29, 2025
7 checks passed
@wojtekmach wojtekmach deleted the wm-pretty-print branch May 29, 2025 09:44
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.

2 participants