fschuindt

fschuindt

Struggling to update embedded schemas

Hi there, folks. I’m struggling with this issue for quite some time… It’s with Ecto embedded schemas…

Consider the following:
Orders embeds many Items.

I have the following on the Order schema:
embeds_many :items, Schemas.Order.Item, on_replace: :delete

In the same Schema, I have created the following function:

  @doc """
  Updates a given `t:OPS.DataBase.Schemas.Order/0` in the
  database. (Permits the update of the "items" embeded schema)
  """
  @spec update_with_items(t(), map(), list(Schemas.Order.Item.t())) :: response()
  def update_with_items(order, params, items) do
    order
    |> cast(params, @update_params)
    |> put_embed(:items, items)
    |> Repo.update()
  end

So for a given Order, I can update some parameters on it, but also can update the list of Items.
It does update the parameters, but will not update the list of items.

Here’s a example of the list of items I’m passing to the function:

[
  %OPS.DataBase.Schemas.Order.Item{
    id: "4e903100-07ff-4096-b0f0-8bc88c50c630",
    image: "image",
    name: "item 1",
    picked: false,
    price: nil,
    product_id: 987654,
    quantity: 2,
    supplier_store_id: "64ac2f41-dcf4-43d1-82b7-da994c99ba57",
    title: nil,
    variant_id: 99999,
    variant_title: "variant_title",
    vendor: "vendor"
  },
  %OPS.DataBase.Schemas.Order.Item{
    id: "b8972930-9d94-470a-90ad-abcf67c3c57e",
    image: "image",
    name: "item 2",
    picked: false,
    price: nil,
    product_id: 657321,
    quantity: 2,
    supplier_store_id: "454545454545",
    title: nil,
    variant_id: 88888,
    variant_title: "variant_title",
    vendor: "vendor"
  }
]

This is during my tests, and it’s not updating the Items of an Order.
Note that those items contains the ID already, I don’t think those matter to be honest, but I’m about to remove them to see if it works.

I’m kinda clueless here, any help/tip will be welcome.

Thank you!

First Post!

tfwright

tfwright

Passing a list including ids should work fine. I haven’t had any trouble doing that. What is the output of the function? If it’s not raising an error, my first guess would be this behavior described in the docs:

If the embed has no changes, it will be skipped.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement