bian2510
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?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
What does your database say? Does that property object actually have linked address?
al2o3cr
There’s something odd going on - the printout shows
nilinaddress, but the error message is from inside a protocol implementation forEcto.Association.NotLoaded:https://github.com/elixir-ecto/ecto/blob/74a85a99b6dc0fc4667876d5341ad4cafceaa9f6/lib/ecto/json.ex#L2-L6
The code in
create_propertylooks like it should return a correctly loaded association (nilis still “loaded”, just empty), so the next place to investigate is where the result fromcreate_propertyis used.bian2510
is used in a test:
user_controller_text.exs
This is property_controller.ex
crud_base.ex
al2o3cr
I suspect the extra
[ ]aroundpreloadshere is confusingRepo.preload.bian2510
Yes this is the solution for this test. Thanks. But I’ve other test that is failing could you please help me with this?
this is the update method
this is the update function in the property_controller.ex
this is the update method in my crud_base.ex
and this is the changeset
The endpoint is working but the test fail with the same error I can’t identify the error.
You have any idea?
bian2510
The problem is here because if I try get and user with properties created need the preload to show this complete entity.
When I try of return one user with properties and this properties not have the preloads
here is the example
The question is. How I could preload the attrs of the property from this place?
al2o3cr
Repo.preloadcan handle nested associations. If you need thepropertiesfor thatUserto have a loadedaddressassociation, you’d write something like:bian2510
Thank you very much for the help, with this I was able to fix the error.
