I’ve been trying to connect 2 tables (for over a week) using a join table as described in Ecto Association Guide – Ecto v2.2.11. Best docs I could find.
I have three tables: organizations
, employees
, organization_employees
. There is no schema for organization_employees
The latest issue is a PK error. I insert and preload the “objects”, but when I try to update the DB with put_assoc
I get an error. The “objects” do exist in the DB and PK matches.
A breif snipped is below while the full code is here: Loading put_assoc · GitHub
org_update = Ecto.Changeset.put_assoc(org_changeset, :employees, [e_insert])
#Ecto.Changeset<
action: nil,
changes: %{
email: "toys@rus.com",
employees: [
#Ecto.Changeset<action: :update, changes: %{}, errors: [],
data: #Employee<>, valid?: true>
],
name: "Toys R Us",
phone: "777777777",
slug: "toys-r-us"
},
errors: [],
data: .Organization<>,
valid?: true
>
Repo.update(org_update)
** (Ecto.NoPrimaryKeyValueError) struct `%TurnStile.Company.Organization{__meta__: #Ecto.Schema.Metadata<:built, "organizations">, id: nil, email: nil, name: nil, slug: nil, phone: nil, employees: #Ecto.Association.NotLoaded<association :employees is not loaded>, inserted_at: nil, updated_at: nil}` is missing primary key value
(ecto 3.9.4) lib/ecto/repo/schema.ex:977: anonymous fn/3 in Ecto.Repo.Schema.add_pk_filter!/2
(elixir 1.14.1) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto 3.9.4) lib/ecto/repo/schema.ex:416: Ecto.Repo.Schema.do_update/4
iex:13: (file)
You can see in the full code that I do preload, so I don’t know why it says employees is not loaded
. Even after preloading, employees
is an empy list []
, even though there is an employee
in the DB. Vice versa for organizations
. Not sure if this is how it’s supposed to be.
This has been SO error prone and tedious. Any help is appreciated.