From 40481a17a04711aba8f2d7be804b49227fd537ea Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Wed, 6 Mar 2024 08:23:56 -0500 Subject: [PATCH] fix: prevent ecto/pg from getting confused about the type of maps --- lib/expr.ex | 11 +++++++---- test/bulk_update_test.exs | 11 +++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/expr.ex b/lib/expr.ex index 31d9893..431a115 100644 --- a/lib/expr.ex +++ b/lib/expr.ex @@ -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 diff --git a/test/bulk_update_test.exs b/test/bulk_update_test.exs index 0bc8206..d561167 100644 --- a/test/bulk_update_test.exs +++ b/test/bulk_update_test.exs @@ -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)