bian2510
** (RuntimeError) cannot encode association :address from MyApp.Model.Property to JSON because the association was not loaded
Hi guys I’m having troubles with one preload.
I’m creating one property and I’m adding preloads, but when not have any register for has_one association is returning nil and through this error
** (RuntimeError) cannot encode association :address from MyApp.Model.Property to JSON because the association was not loaded.
You can either preload the association:
Repo.preload(MyApp.Model.Property, :address)
Or choose to not encode the association when converting the struct to JSON by explicitly listing the JSON fields in your schema:
defmodule MyApp.Model.Property do
# ...
@derive {Jason.Encoder, only: [:name, :title, ...]}
schema ... do
I’ve print the Property created and looks like this
%MyApp.Model.Property{
__meta__: #Ecto.Schema.Metadata<:loaded, "properties">,
address: nil,
currency: "USD",
description: "Depto 2 ambientes",
id: 908,
inserted_at: ~N[2021-05-14 15:29:52],
price: 260000,
property_features: nil,
property_images: [],
property_type: "department",
spaces: 2,
status: true,
subscriptions: [],
updated_at: ~N[2021-05-14 15:29:52],
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 3093
}
I think that this could be for the nil returned in the property?
This is my function to create the property.
def create_property(user) do
Repo.insert!(%Property{
description: "Depto 2 ambientes",
price: 260_000,
currency: "USD",
spaces: 2,
status: true,
property_type: "department",
user_id: user.id
}) |> Repo.preload([:address, :subscriptions, :property_features, :property_images]) |> IO.inspect
end
and this is the schema in property.ex
@derive {Jason.Encoder, except: [:__meta__, :inserted_at, :updated_at, :user]}
schema "properties" do
field :spaces, :integer
field :currency, :string
field :description, :string
field :price, :integer
field :property_type, :string
field :status, :boolean, default: false
belongs_to :user, User
has_one :address, Address
has_one :property_features, PropertyFeatures
has_many :subscriptions, Subscription
has_many :property_images, PropertyImages
timestamps()
end
Any have idea where is the problem?
Marked As Solved
al2o3cr
Repo.preload can handle nested associations. If you need the properties for that User to have a loaded address association, you’d write something like:
|> Repo.preload([properties: [:address]])
Also Liked
al2o3cr
There’s something odd going on - the printout shows nil in address, but the error message is from inside a protocol implementation for Ecto.Association.NotLoaded:
https://github.com/elixir-ecto/ecto/blob/74a85a99b6dc0fc4667876d5341ad4cafceaa9f6/lib/ecto/json.ex#L2-L6
The code in create_property looks like it should return a correctly loaded association (nil is still “loaded”, just empty), so the next place to investigate is where the result from create_property is used.
al2o3cr
Last Post!
bian2510
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









