Skip to content

Commit

Permalink
Raise when empty list is given to values/2 (#4523)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski authored Oct 8, 2024
1 parent 23da400 commit a1a5047
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ defmodule Ecto.Query do
@moduledoc false
defstruct [:types, :num_rows, :params]

def new([], _types) do
raise ArgumentError, "must provide a non-empty list to values/2"
end

def new(values_list, types) do
fields = fields(values_list)
types = types!(fields, types)
Expand Down
3 changes: 2 additions & 1 deletion lib/ecto/query/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ defmodule Ecto.Query.API do
and `Ecto.Query.join/5`.
The first argument is a list of maps representing the values of the constant table.
Each entry in the list must have exactly the same fields or an error is raised.
An error is raised if the list is empty or if every map does not have exactly the
same fields.
The second argument is a map of types corresponding to the fields in the first argument.
Each field must be given a type or an error is raised. Any type that can be specified in
Expand Down
8 changes: 8 additions & 0 deletions test/ecto/query/builder/from_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ defmodule Ecto.Query.Builder.FromTest do
types_kw = Enum.map(types, & &1)
assert query.from.source == {:values, [], [types_kw, length(values)]}

# Empty values
msg = "must provide a non-empty list to values/2"

assert_raise ArgumentError, msg, fn ->
from v in values([], %{})
end


# Missing type
msg = "values/2 must declare the type for every field. The type was not given for field `text`"

Expand Down

0 comments on commit a1a5047

Please sign in to comment.