Skip to content

Commit

Permalink
Improve error message on unallowed preload, closes #2926
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Apr 2, 2019
1 parent a35cd8c commit f4581d5
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/ecto/query/planner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ defmodule Ecto.Query.Planner do
defp plan_assocs(_query, _ix, []), do: :ok
defp plan_assocs(query, ix, assocs) do
# We validate the schema exists when preparing joins above
{_, parent_schema, _} = get_source!(:preload, query, ix)
{_, parent_schema, _} = get_preload_source!(query, ix)

Enum.each assocs, fn {assoc, {child_ix, child_assocs}} ->
refl = parent_schema.__schema__(:association, assoc)
Expand Down Expand Up @@ -1228,19 +1228,14 @@ defmodule Ecto.Query.Planner do
end

defp collect_assocs(exprs, fields, query, tag, take, [{assoc, {ix, children}}|tail]) do
case get_source!(:preload, query, ix) do
{source, schema, _} = to_take when is_binary(source) and schema != nil ->
{fetch, take_children} = fetch_assoc(tag, take, assoc)
{expr, taken} = take!(to_take, query, fetch, assoc, ix)
exprs = [expr | exprs]
fields = Enum.reverse(taken, fields)
{exprs, fields} = collect_assocs(exprs, fields, query, tag, take_children, children)
{exprs, fields} = collect_assocs(exprs, fields, query, tag, take, tail)
{exprs, fields}
_ ->
error! query, "can only preload sources with a schema " <>
"(fragments, binary and subqueries are not supported)"
end
to_take = get_preload_source!(query, ix)
{fetch, take_children} = fetch_assoc(tag, take, assoc)
{expr, taken} = take!(to_take, query, fetch, assoc, ix)
exprs = [expr | exprs]
fields = Enum.reverse(taken, fields)
{exprs, fields} = collect_assocs(exprs, fields, query, tag, take_children, children)
{exprs, fields} = collect_assocs(exprs, fields, query, tag, take, tail)
{exprs, fields}
end
defp collect_assocs(exprs, fields, _query, _tag, _take, []) do
{exprs, fields}
Expand Down Expand Up @@ -1320,6 +1315,16 @@ defmodule Ecto.Query.Planner do
"in `#{where}` (look for `unknown_binding!` in the printed query below)"
end

defp get_preload_source!(query, ix) do
case get_source!(:preload, query, ix) do
{source, schema, _} = all when is_binary(source) and schema != nil ->
all
_ ->
error! query, "can only preload sources with a schema " <>
"(fragments, binary and subqueries are not supported)"
end
end

## Helpers

@exprs [distinct: :distinct, select: :select, from: :from, join: :joins,
Expand Down Expand Up @@ -1489,7 +1494,7 @@ defmodule Ecto.Query.Planner do
end

defp filter_and_reraise(exception, stacktrace) do
reraise exception, Enum.reject(stacktrace, &match?({__MODULE__, _, _, _}, &1))
reraise exception, stacktrace # Enum.reject(stacktrace, &match?({__MODULE__, _, _, _}, &1))

This comment has been minimized.

Copy link
@AndrewDryga

AndrewDryga Apr 2, 2019

Contributor

@josevalim I think you forgot to un-comment stacktrace prunning here

This comment has been minimized.

Copy link
@josevalim

josevalim Apr 2, 2019

Member

Thank you, good catch.

end

defp error!(query, message) do
Expand Down

0 comments on commit f4581d5

Please sign in to comment.