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
countryfromFoothat 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
You cannot have associations in embeds, as this is not something databases support.
Popular in Questions
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
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
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
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
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
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...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
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
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
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







