chrisdel101

chrisdel101

Ecto.NoPrimaryKeyValueError: ecto join and many-to-many

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.

Marked As Solved

tfwright

tfwright

The only technical problem in your code causing the error you are getting is that you are passing an incorrect changeset to put_assoc. The original changeset used for the insert will not have the id because that is loaded only after the insert. This is the relevant example code from the guide you are following:

post_changeset = Ecto.Changeset.change(post)

post_with_tags = Ecto.Changeset.put_assoc(post_changeset, :tags, [clickbait_tag, misc_tag])

post = Repo.update!(post_with_tags)

Notice that the new changeset for the update is created using change on the result of the parent insert, and then that is passed to put_assoc

Also Liked

tfwright

tfwright

edit

Sorry, I was having a hard time following your code, you need to build a changeset with the org_insert struct. It is hard to be more specific without seeing even more code though, since this is not an idiomatic way to insert a parent and children. Probably you want cast_assoc on the original changeset instead of two separate inserts, or possibly a multi.

Docs for cast_assoc might help: Ecto.Changeset — Ecto v3.14.0

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement