thojanssens1

thojanssens1

Preload associations in embeds?

Say I have the following schemas:

defmodule Articles do
  schema "articles" do
    field(:name, :string)
    embeds_one(:foo, Foo)
  end

  def changeset(struct, attrs) do
    struct
    |> cast(attrs, [:name])
    |> cast_embed(:foo)
  end
end
defmodule Foo do
  @primary_key false

  embedded_schema do
    field(:name, :string)
    belongs_to(:country, Country)
  end

  def changeset(struct, attrs) do
    struct
    |> cast(attrs, [:name])
    |> cast_assoc(:country)
  end
end
defmodule Country do
  schema "countries" do
    field :name, :string
  end
end

If I try to run the code below, I’ll have the error:

** (RuntimeError) attempting to cast or change association country from Foo that was not loaded. Please preload your associations before manipulating them thr
ough changesets

attrs = %{
  name: "An article",
  foo: %{
    country: %{name: "Fiji"}
  }
}

%Article{}
|> Article.changeset(attrs)
|> Repo.insert!()

If I try to preload:

%Article{}
|> Repo.preload([foo: :country])

I’ll have the error:

** (ArgumentError) schema Article does not have association :foo

First Post!

LostKobrakai

LostKobrakai

You cannot have associations in embeds, as this is not something databases support.

Where Next?

Popular in Questions Top

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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
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
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