Skip to content

Commit 0917cbb

Browse files
authored
Improve docs and error for Ecto.Repo.delete_all/2 (#4211)
1 parent 2f730ae commit 0917cbb

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

lib/ecto/query/planner.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,10 @@ defmodule Ecto.Query.Planner do
19951995
havings: [], preloads: [], assocs: [], distinct: nil, lock: nil,
19961996
windows: [], combinations: []} ->
19971997
query
1998+
_ when operation == :delete_all ->
1999+
error! query, "`#{operation}` allows only `with_cte`, `where`, `select`, and `join` expressions. " <>
2000+
"You can exclude unwanted expressions from a query by using " <>
2001+
"Ecto.Query.exclude/2. Error found"
19982002
_ ->
19992003
error! query, "`#{operation}` allows only `with_cte`, `where` and `join` expressions. " <>
20002004
"You can exclude unwanted expressions from a query by using " <>

lib/ecto/repo.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,11 @@ defmodule Ecto.Repo do
12021202
12031203
MyRepo.delete_all(Post)
12041204
1205-
from(p in Post, where: p.id < 10) |> MyRepo.delete_all
1205+
from(p in Post, where: p.id < 10) |> MyRepo.delete_all()
1206+
1207+
# With returning results, if supported by the database.
1208+
{_count, posts} = from(p in Post, where: p.id < 10, select: p) |> MyRepo.delete_all()
1209+
12061210
"""
12071211
@doc group: "Query API"
12081212
@callback delete_all(queryable :: Ecto.Queryable.t(), opts :: Keyword.t()) ::

test/ecto/query/planner_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,7 @@ defmodule Ecto.Query.PlannerTest do
17601760
from(p in Post, update: [set: [name: "foo"]]) |> normalize(:delete_all)
17611761
end
17621762

1763-
message = ~r"`delete_all` allows only `with_cte`, `where` and `join` expressions"
1763+
message = ~r"`delete_all` allows only `with_cte`, `where`, `select`, and `join` expressions"
17641764
assert_raise Ecto.QueryError, message, fn ->
17651765
from(p in Post, order_by: p.title) |> normalize(:delete_all)
17661766
end

0 commit comments

Comments
 (0)