JSONB <=> Map fails on Array

I have a column of type jsonb. Let's call this column metadata In my resource, I define the attributes as
attributes do
integer_primary_key :id
attribute :metadata, :map
end
attributes do
integer_primary_key :id
attribute :metadata, :map
end
If my metadata is a json element, the data is returned properly. However, if the metadata is an array of json elements, the Ash Query fails. Sample error response
[debug] QUERY OK source="maptest" db=205.5ms queue=0.1ms idle=942.4ms
SELECT m0."id", m0."metadata" FROM "maptest" AS m0 WHERE (m0."id"::bigint = $1::bigint) [1]
↳ AshPostgres.DataLayer.run_query/2, at: lib/data_layer.ex:628
** (ArgumentError) cannot load `[%{"Amount" => 925.93, "Description" => "PROPOSED INTERIOR DESIGN & RENOVATION", "DescriptionLineDetail" => nil, "DetailType" => "SalesItemLineDetail", "DiscountLineDetail" => nil, "Id" => "1", "LineNum" => "1", "SalesItemLineDetail" => %{"ClassRef" => nil, "Description" => nil, "ItemAccountRef" => %{"name" => "Sales - Renovation", "value" => "6"}, "ItemRef" => %{"name" => "Sales - Renovation", "value" => "6"}, "Qty" => 1.0, "ServiceDate" => "2023-02-26T00:00:00.000000Z", "TaxCodeRef" => %{"value" => "27"}, "UnitPrice" => 925.93}, "SubTotalLineDetail" => nil}, %{"Amount" => 925.93, "Description" => nil, "DescriptionLineDetail" => nil, "DetailType" => "SubTotalLineDetail", "DiscountLineDetail" => nil, "Id" => nil, "LineNum" => nil, "SalesItemLineDetail" => nil, "SubTotalLineDetail" => %{}}]` as type #Ash.Type.Map.EctoType<[]> for field :metadata in #Vosmos.Sale.Resources.MapTest<__meta__: #Ecto.Schema.Metadata<:loaded, "maptest">, id: nil, metadata: nil, aggregates: %{}, calculations: %{}, __order__: nil, ...>
(ecto 3.10.1) lib/ecto/repo/queryable.ex:419: Ecto.Repo.Queryable.struct_load!/6
(ecto 3.10.1) lib/ecto/repo/queryable.ex:243: anonymous fn/5 in Ecto.Repo.Queryable.preprocessor/3
(elixir 1.14.0) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2.....
[debug] QUERY OK source="maptest" db=205.5ms queue=0.1ms idle=942.4ms
SELECT m0."id", m0."metadata" FROM "maptest" AS m0 WHERE (m0."id"::bigint = $1::bigint) [1]
↳ AshPostgres.DataLayer.run_query/2, at: lib/data_layer.ex:628
** (ArgumentError) cannot load `[%{"Amount" => 925.93, "Description" => "PROPOSED INTERIOR DESIGN & RENOVATION", "DescriptionLineDetail" => nil, "DetailType" => "SalesItemLineDetail", "DiscountLineDetail" => nil, "Id" => "1", "LineNum" => "1", "SalesItemLineDetail" => %{"ClassRef" => nil, "Description" => nil, "ItemAccountRef" => %{"name" => "Sales - Renovation", "value" => "6"}, "ItemRef" => %{"name" => "Sales - Renovation", "value" => "6"}, "Qty" => 1.0, "ServiceDate" => "2023-02-26T00:00:00.000000Z", "TaxCodeRef" => %{"value" => "27"}, "UnitPrice" => 925.93}, "SubTotalLineDetail" => nil}, %{"Amount" => 925.93, "Description" => nil, "DescriptionLineDetail" => nil, "DetailType" => "SubTotalLineDetail", "DiscountLineDetail" => nil, "Id" => nil, "LineNum" => nil, "SalesItemLineDetail" => nil, "SubTotalLineDetail" => %{}}]` as type #Ash.Type.Map.EctoType<[]> for field :metadata in #Vosmos.Sale.Resources.MapTest<__meta__: #Ecto.Schema.Metadata<:loaded, "maptest">, id: nil, metadata: nil, aggregates: %{}, calculations: %{}, __order__: nil, ...>
(ecto 3.10.1) lib/ecto/repo/queryable.ex:419: Ecto.Repo.Queryable.struct_load!/6
(ecto 3.10.1) lib/ecto/repo/queryable.ex:243: anonymous fn/5 in Ecto.Repo.Queryable.preprocessor/3
(elixir 1.14.0) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2.....
1 Reply
Terry Lee
Terry LeeOP2y ago
My bad. I just found out I need a composite type. { :array, :map}

Did you find this page helpful?