From 90aa4aec96570f312751cf22b5c27f8f05d133b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Bara=C3=BAna?= Date: Wed, 3 Dec 2025 13:28:47 -0300 Subject: [PATCH] Handle empty list argument without raising error --- lib/ecto/repo/queryable.ex | 4 ++++ test/ecto/repo_test.exs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/ecto/repo/queryable.ex b/lib/ecto/repo/queryable.ex index c60989570f..b6b9614f4a 100644 --- a/lib/ecto/repo/queryable.ex +++ b/lib/ecto/repo/queryable.ex @@ -90,6 +90,8 @@ defmodule Ecto.Repo.Queryable do one!(name, query_for_get_by(queryable, clauses), opts) end + def reload(_name, [], _opts), do: [] + def reload(name, [head | _] = structs, opts) when is_list(structs) do results = all(name, query_for_reload(structs), opts) @@ -105,6 +107,8 @@ defmodule Ecto.Repo.Queryable do one(name, query_for_reload([struct]), opts) end + def reload!(_name, [], _opts), do: [] + def reload!(name, [head | _] = structs, opts) when is_list(structs) do query = query_for_reload(structs) results = all(name, query, opts) diff --git a/test/ecto/repo_test.exs b/test/ecto/repo_test.exs index 36af5d42e4..805997ce6d 100644 --- a/test/ecto/repo_test.exs +++ b/test/ecto/repo_test.exs @@ -350,6 +350,14 @@ defmodule Ecto.RepoTest do TestRepo.reload(struct_with_custom_source) assert_received {:all, %{from: %{source: {"custom_schema", MySchema}}}} end + + test "returns empty list when given empty list" do + assert TestRepo.reload([]) == [] + end + + test "reload! returns empty list when given empty list" do + assert TestRepo.reload!([]) == [] + end end defmodule DefaultOptionRepo do