fix: prevent ecto/pg from getting confused about the type of maps

This commit is contained in:
Zach Daniel 2024-03-06 08:23:56 -05:00
parent 1a4a508e39
commit 40481a17a0
2 changed files with 18 additions and 4 deletions

View file

@ -1594,10 +1594,13 @@ defmodule AshPostgres.Expr do
defp do_dynamic_expr(query, value, bindings, embedded?, acc, _type)
when is_map(value) and not is_struct(value) do
Enum.reduce(value, {%{}, acc}, fn {key, value}, {map, acc} ->
{value, acc} = do_dynamic_expr(query, value, bindings, embedded?, acc)
{Map.put(map, key, value), acc}
end)
{value, acc} =
Enum.reduce(value, {%{}, acc}, fn {key, value}, {map, acc} ->
{value, acc} = do_dynamic_expr(query, value, bindings, embedded?, acc)
{Map.put(map, key, value), acc}
end)
{Ecto.Query.dynamic([], type(^value, :map)), acc}
end
defp do_dynamic_expr(query, other, bindings, true, acc, type) do

View file

@ -34,6 +34,17 @@ defmodule AshPostgres.BulkUpdateTest do
|> Enum.map(& &1.list_of_stuff)
end
test "a map can be given as input on a regular update" do
%{records: [post | _]} =
Api.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create,
return_records?: true
)
post
|> Ash.Changeset.for_update(:update, %{list_of_stuff: [%{a: [:a, :b]}, %{a: [:c, :d]}]})
|> Api.update!()
end
test "bulk updates only apply to things that the query produces" do
Api.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create)