The name of the module is singular, the name of the schema is plural… here is an example of a User with many events.
defmodule Koko.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
alias Koko.Core.Event
schema "users" do
has_many(:events, Event)
...
end
end
and how to test in the console
$ iex -S mix
iex(1)> u = Accounts.get_user_by_name "admin"
[debug] QUERY OK source="users" db=9.6ms decode=1.4ms queue=1.7ms idle=458.2ms
SELECT u0."id", u0."name", u0."email", u0."password_hash", u0."roles_mask", u0."sign_in_count", u0."current_sign_in_at", u0."last_sign_in_at", u0."current_sign_in_ip", u0."last_sign_in_ip", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."name" = $1) ["admin"]
%App4am.Accounts.User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
current_sign_in_at: nil,
current_sign_in_ip: nil,
email: "admin@example.local",
events: #Ecto.Association.NotLoaded<association :events is not loaded>,
id: "55f25ed1-6f5c-4754-b90d-13e07efb606d",
inserted_at: ~U[2021-08-21 17:41:29Z],
last_sign_in_at: nil,
last_sign_in_ip: nil,
name: "admin",
password: nil,
password_hash: "$argon2id$v=19$m=131072,t=8,p=4$XnOozYyAhLuuvyvLODb8mA$Q3DhosYC+ZXDYt9oZyrTfFaeFPiqPiUWf6mt8Qua54k",
roles: nil,
roles_mask: 1,
sign_in_count: 0,
updated_at: ~U[2021-08-21 17:41:29Z]
}
iex(2)> Ecto.build_assoc u, :events
%App4am.Core.Event{
__meta__: #Ecto.Schema.Metadata<:built, "events">,
expiry_date: nil,
id: nil,
inserted_at: nil,
medium: nil,
publication_date: nil,
publication_status: :draft,
thumbnail: nil,
title: nil,
updated_at: nil,
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: "55f25ed1-6f5c-4754-b90d-13e07efb606d"
}
Of course those are my demo data.