Why is module queryable

Hi all
I have a model User that looks as follow:

defmodule Rumbl.User do

  use Rumbl.Web, :model

  schema "users" do
    field :name, :string
    field :username, :string
    field :password, :string, virtual: true
    field :password_hash, :string
    has_many :videos, Rumbl.Video



When I want to query all data from the repository, I did with following statement:

iex(6)> Repo.all(Rumbl.User)
[debug] QUERY OK db=2.1ms decode=5.8ms
SELECT u0."id", u0."name", u0."username", u0."password_hash", u0."inserted_at", u0."updated_at" FROM "users" AS u0 []
[%Rumbl.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">, id: 1,
  inserted_at: #Ecto.DateTime<2016-07-09 21:23:04>, name: "Jose Valim",
  password: nil,
  password_hash: "$2b$12$g58Uu33RoDatb7P6OWPWROzLvuSmbD48naWSenFQYhUs.RpKU54SO",
  updated_at: #Ecto.DateTime<2016-07-09 21:23:04>, username: "josevalim",
  videos: #Ecto.Association.NotLoaded<association :videos is not loaded>},

When i look at the definition of all function https://hexdocs.pm/ecto/Ecto.Repo.html#c:all/2, it expects a queryable type.
Is a schema(struct) queryable, like Rumbl.User schema?


1 Like

Yes a module that has a schema is queryable. It is possible to be queryable because https://github.com/elixir-ecto/ecto/blob/fb397116e25733d27b64e75df9ccef1e603121e0/lib/ecto/queryable.ex#L25 implements the queryable protocol for atoms, and module names are atoms.


Thanks so much.