From 14e5be42cc58c1292fe6fe22e166e32a072a328b Mon Sep 17 00:00:00 2001 From: Antoine Bolvy Date: Thu, 29 Feb 2024 10:04:32 +0100 Subject: [PATCH 1/2] Mention that update expressions are composable (closes #4385) --- lib/ecto/query.ex | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/ecto/query.ex b/lib/ecto/query.ex index 9f3fbd0fe8..8622a9e122 100644 --- a/lib/ecto/query.ex +++ b/lib/ecto/query.ex @@ -2534,6 +2534,19 @@ defmodule Ecto.Query do from(u in User, update: [pull: [tags: "not cool"]]) + ## Composable + + Remember that all query expressions are composable, so you can use `update` + multiple times in the same query to merge the update expressions: + + new_name = "new name" + User + |> update([u], set: [name: fragment("upper(?)", ^new_name)]) + |> update([u], set: [age: 42]) + + This can be useful if the expressions are complex as Ecto's compiler is kept + simple and only understands a subset of Elixir's syntax. + """ defmacro update(query, binding \\ [], expr) do Builder.Update.build(query, binding, expr, __CALLER__) From 619fe1004b7487561d1af4e4c51178c662860803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 29 Feb 2024 10:22:58 +0100 Subject: [PATCH 2/2] Update lib/ecto/query.ex --- lib/ecto/query.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ecto/query.ex b/lib/ecto/query.ex index 8622a9e122..29aa264c9a 100644 --- a/lib/ecto/query.ex +++ b/lib/ecto/query.ex @@ -2544,9 +2544,9 @@ defmodule Ecto.Query do |> update([u], set: [name: fragment("upper(?)", ^new_name)]) |> update([u], set: [age: 42]) - This can be useful if the expressions are complex as Ecto's compiler is kept - simple and only understands a subset of Elixir's syntax. - + This can be useful to compose updates from different functions + or when mixing interpolation, such as `set: ^updates`, with regular + query expressions, such as `set: [age: u.age + 1]`. """ defmacro update(query, binding \\ [], expr) do Builder.Update.build(query, binding, expr, __CALLER__)