WammKD

WammKD

Put_assoc Cannot Perform Update to Many-to-Many

So I have a table of ItemA which has a many-to-many mapping to ItemB.

In the changeset of ItemA, the ItemB association gets preloaded and, then, the relevant ItemBs from the database are added via put_assoc().

When handling a list of ItemB that’s already associated with ItemA, the rows linking them are DELETEd from the mapping table (this makes sense as on_replace: is set to :delete).

However, they are, then, never INSERTed again, leaving my put_assoc() to not associate the struct.s I’ve provided with ItemA but to actively destroy the mappings.

Given that the association has no changes, I would expect (as the documentation says) for this to be skipped.

First Post!

WammKD

WammKD

I didn’t think I was doing anything particularly complicated; the ItemA schema looks like

defmodule ItemA do
  use    Ecto.Schema;
  import Ecto.Changeset;

  schema "item_a" do
    …

    many_to_many(:item_bs,                         ItemB,
                 join_through: "item_a_to_item_b", on_replace: :delete);

    timestamps();
  end

  def changeset(%ItemA{} = a, attrs) do
    a |> cast(attrs, […])    # these are actual struct.s from the database, in the code
      |> put_assoc(:item_bs, [%ItemB{}, %ItemB{}, %ItemB{}, %ItemB{}])
  end
end

Yet, after a successful first INSERT, any subsequent UPDATEs DELETEs all entries from the mapping table while the next UPDATE INSERTs them into the mapping table while the next UPDATE DELETEs them while the next UPDATE

I just want it to get the database to match the modules/structs I give it, not do the literal opposite.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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 record...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement