Skip to content

Error when creating/updating an :embedded attribute #31

Open
@diogomrts

Description

@diogomrts

Describe the bug
We have a simple resource which has a jsonb column in Postgres, we have been using it for a while now. This column is described as:

attribute :csv_column_mappings, {:array, Type.CSVColumnMapping}, public?: true

After enabling AshEvents for this resource we started getting an encoding error on this attribute:

** (Ash.Error.Unknown) 
Bread Crumbs:
  > Error returned from: Helpdesk.Support.PgFile.create

Unknown Error

* ** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for type Type.CSVColumnMapping (a struct), Jason.Encoder protocol must always be explicitly implemented.

If you own the struct, you can derive the implementation specifying which fields should be encoded to JSON:

    @derive {Jason.Encoder, only: [....]}
    defstruct ...

It is also possible to encode all fields, although this should be used carefully to avoid accidentally leaking private information when new fields are added:

    @derive Jason.Encoder
    defstruct ...

Finally, if you don't own the struct you want to encode to JSON, you may use Protocol.derive/3 placed outside of any module:

    Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...])
    Protocol.derive(Jason.Encoder, NameOfTheStruct)


Got value:

    %Type.CSVColumnMapping{
      column: 0,
      attribute: "contact",
      __meta__: #Ecto.Schema.Metadata<:built, "">
    }

  (jason 1.4.4) lib/jason.ex:213: Jason.encode_to_iodata!/2
  (postgrex 0.20.0) lib/postgrex/type_module.ex:1084: Postgrex.DefaultTypes.encode_params/3
  (postgrex 0.20.0) lib/postgrex/query.ex:75: DBConnection.Query.Postgrex.Query.encode/3
  (db_connection 2.7.0) lib/db_connection.ex:1449: DBConnection.encode/5
  (db_connection 2.7.0) lib/db_connection.ex:1549: DBConnection.run_prepare_execute/5
  (db_connection 2.7.0) lib/db_connection.ex:772: DBConnection.parsed_prepare_execute/5
  (db_connection 2.7.0) lib/db_connection.ex:764: DBConnection.prepare_execute/4
  (postgrex 0.20.0) lib/postgrex.ex:296: Postgrex.query/4
    (ash 3.5.17) lib/ash/error/unknown.ex:3: Ash.Error.Unknown."exception (overridable 2)"/1
    (ash 3.5.17) /Users/diogomartins/Projects/test_repo/deps/splode/lib/splode.ex:264: Ash.Error.to_class/2
    (ash 3.5.17) lib/ash/error/error.ex:108: Ash.Error.to_error_class/2
    (ash 3.5.17) lib/ash/actions/create/create.ex:161: Ash.Actions.Create.do_run/4
    (ash 3.5.17) lib/ash/actions/create/create.ex:50: Ash.Actions.Create.run/4
    (ash 3.5.17) lib/ash.ex:3039: Ash.create!/3
    iex:1: (file)

To Reproduce
Try to perform any create/update with the attribute csv_column_mappings in this file pg_file.ex on this repro: https://github.com/diogomrts/test_repo/tree/ash-events-embedded-resource

If we uncomment # ignore_actions([:create, :update]) it works as normal

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions