Issue: FK fields of many to many table remain null I have a simple many-to-many table to join a Users table and an Employers table:
Users table:
schema "users" do
field :email, :string
field :first_name, :string
field :hashed_password, :string, redact: true
field :password, :string, virtual: true
field :password_confirmation, :string, virtual: true
field :is_active, :boolean, default: false
field :last_name, :string
field :middle_name, :string
many_to_many :employers, Employer, join_through: UserEmployer
timestamps()
end
Employers table:
schema "employers" do
field :employer_name, :string
many_to_many :users, User, join_through: UserEmployer
timestamps()
end
UsersEmployers table:
schema "users_employers" do
belongs_to :users, User
belongs_to :employers, Employer
timestamps()
end
To use this association in iex, I'm grabbing a User (user1) and an Employer (employer1) and running:
user1
|> Repo.preload(:employers)
|> User.changeset(%{})
|> Ecto.Changeset.put_assoc(:employers, [employer1])
|> Repo.update!
The output:
17:01:23.896 [debug] QUERY OK db=0.8ms
INSERT INTO "users_employers" ("inserted_at","updated_at","id") VALUES ($1,$2,$3) [~N[2021-05-17 17:01:23], ~N[2021-05-17 17:01:23], <<97, 185, 97, 180, 30, 80, 73, 50, 130, 255, 57, 243, 107, 136, 85, 74>>]
17:01:23.898 [debug] QUERY OK db=2.3ms
commit []
%DB.Users.User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
email: "mafia4lye@gmail.com",
employers: [
%DB.Employers.Employer{
__meta__: #Ecto.Schema.Metadata<:loaded, "employers">,
employer_name: "Employer Number 1",
id: "0ed08a40-7315-4c90-a2cf-7d66cb5b7b89",
inserted_at: ~N[2021-05-17 16:31:41],
updated_at: ~N[2021-05-17 16:31:41],
users: #Ecto.Association.NotLoaded<association :users is not loaded>
}
],
first_name: "Vito",
hashed_password: "not_hashed_yet",
id: "9e0d7497-0fb5-48f6-a0ba-7c6af2f6daf7",
inserted_at: ~N[2021-05-17 16:31:42],
is_active: true,
last_name: "Corleone",
middle_name: "Mob",
password: nil,
password_confirmation: nil,
updated_at: ~N[2021-05-17 16:31:42]
}
The row is inserted into the UsersEmployers table, however, the FK's user_id and employer_id are null.
You should use the "singular" form (instead of the "plural" form) in belongs_to
associations:
schema "users_employers" do
belongs_to :user, User
belongs_to :employer, Employer
timestamps()
end