[Proposal] Support lists in clauses of Repo.all_by

15 views
Skip to first unread message

Emmanouil Emmanouilidis

unread,
Jul 3, 2026, 8:11:52 AM (5 days ago) Jul 3
to elixir-ecto
It would be nice as a convenience to be able to do

```elixir
Repo.all_by(Person, email: ["a...@foo.com", "d...@bar.com"])
```

Today the value of each clause is literally compared with a `==`, resulting in messages like

```elixir
iex> Repo.all_by(Person, email: ["a...@foo.com", "d...@bar.com"])

** (Ecto.Query.CastError) deps/ecto/lib/ecto/repo/queryable.ex:25: value `["a...@foo.com", "d...@bar.com"]` in `where` cannot be cast to type :string in query:

from p0 in MyApp.Person,
  prefix: "public",
  where: po.id == ^["a...@foo.com", "d...@bar.com"],
  select: p0
``` 

It would be nice instead if a list was given to generate an `x in [...]` query.

Caveat: this might break usages where the field type is actually an `{:array, ...}` and the user did intent to compare with `==`.

Thanks for reading,
Manos

José Valim

unread,
Jul 3, 2026, 10:22:16 AM (5 days ago) Jul 3
to elixi...@googlegroups.com
The syntax here is explicit by design to avoid ambiguity and surprising security issues. For example, if you do:

    Repo.all_by(email: ^params[:email])

You may have intended a single email but now anyone can pass a list and you have to guarantee your code will work and be safe with both assumptions.

This is why null checks, memberships, and equality are all distinct operators in Ecto.



--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-ecto/33b65545-4bf7-4b3a-aa85-f8caed3a33acn%40googlegroups.com.

Emmanouil Emmanouilidis

unread,
Jul 3, 2026, 10:59:48 AM (5 days ago) Jul 3
to elixir-ecto
Ah, I see, thanks for expanding on where you're coming from. Would including an option like `list_semantics: :equals / :in` (the default being `:equals` so that existing clients don't break), e.g. `Repo.all_by(Person, email: ["a...@foo.com", "d...@bar.com"], list_semantics: :in)` be any better? If I decided to try my hand in this, would it be something interesting for Ecto in general?

José Valim

unread,
Jul 3, 2026, 11:12:41 AM (5 days ago) Jul 3
to elixi...@googlegroups.com
Then you should use the regular query syntax that supports all of those things. We don’t plan to incorporate said options into Ecto!

Reply all
Reply to author
Forward
0 new messages