Skip to content

Commit a1a5047

Browse files
Raise when empty list is given to values/2 (#4523)
1 parent 23da400 commit a1a5047

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

lib/ecto/query.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,10 @@ defmodule Ecto.Query do
485485
@moduledoc false
486486
defstruct [:types, :num_rows, :params]
487487

488+
def new([], _types) do
489+
raise ArgumentError, "must provide a non-empty list to values/2"
490+
end
491+
488492
def new(values_list, types) do
489493
fields = fields(values_list)
490494
types = types!(fields, types)

lib/ecto/query/api.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ defmodule Ecto.Query.API do
515515
and `Ecto.Query.join/5`.
516516
517517
The first argument is a list of maps representing the values of the constant table.
518-
Each entry in the list must have exactly the same fields or an error is raised.
518+
An error is raised if the list is empty or if every map does not have exactly the
519+
same fields.
519520
520521
The second argument is a map of types corresponding to the fields in the first argument.
521522
Each field must be given a type or an error is raised. Any type that can be specified in

test/ecto/query/builder/from_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ defmodule Ecto.Query.Builder.FromTest do
2727
types_kw = Enum.map(types, & &1)
2828
assert query.from.source == {:values, [], [types_kw, length(values)]}
2929

30+
# Empty values
31+
msg = "must provide a non-empty list to values/2"
32+
33+
assert_raise ArgumentError, msg, fn ->
34+
from v in values([], %{})
35+
end
36+
37+
3038
# Missing type
3139
msg = "values/2 must declare the type for every field. The type was not given for field `text`"
3240

0 commit comments

Comments
 (0)