chungwong

chungwong

Ecto recursive(nested) cast_assoc

Given a schema structure:

  schema "sales_item" do
    field :id
    field :product_id
    field :parent_id

    belongs_to :parent, SalesItem, foreign_key: :parent_id
    has_many :children, SalesItem, foreign_key: :parent_id
  end

A product can have many children recursively, i.e. each child can have many children.

And input data as follow

children = [
  {
    "product_id": 12197,
    "children": [
      {
        "product_id": 8637,
        "children": [...]
      },
      {
        "product_id": 11224,
        "children": [...]
      },
      {
        "product_id": 3561
      }
    ]
  },
  {
    "product_id": 12200,
    "children": [
      {
        "product_id": 12129,
        "children": [
          {
            "product_id": 11586,
            "children": [...]
          },
          {
            "product_id": 11377
          },
          {
            "product_id": 10682
          }
        ]
      }
    ]
  }
]

If I have to insert this whole linked data in one go into database,

SalesItem.changeset(%SalesItem{}, %{
                    product_id: 1,
                    children: children,
                   })
                   |> Ecto.Changeset.cast_assoc(:children)

What happened was only the first level of children got inserted, the nested children were ignored completely.

Does cast_assoc support nested associations?

First Post!

ggerico

ggerico

Does cast_assoc support nested associations?

After having read part of Programming Ecto (Pragmatic Bookshelf), I think that you should dig into Transactions and Multi.
Even if they haven’t explicitly said that cast_assoc doesn’t support nested associations.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
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
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
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
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
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
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

We're in Beta

About us Mission Statement