1
votes

I want to fetch all primary keys from a table, but I don't want to fetch any of the other fields from the db. What I've found so far is recommendations to use Repo.all and Map.take but that clearly fetches too much from the database.

How do I fetch a single field from all entries in an ecto schema?

1

1 Answers

5
votes

Ecto.Query.select/3 is your friend.

from u in User, select: u.id

To retrieve several fields, use

from u in User, select: {u.id, u.name}