Search code examples
elixirecto

Fetch all primary keys with Ecto?


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?


Solution

  • 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}