twobeers
i am struggling with ecto these days… i really do not understand concepts ![]()
For a demo I startet a project with a single DB table.. and fine this worked, I was happy what can be done in really a short amount of time with elixir / phoenix.
But then I just want to structure this single Table. One field, named “parameterset” should be populated flexible in a new table parametersets and should be just referenced from my “main” table. (just a first step). Outside (Form view) there is only a selection possible from entries in this table. Both are connected throw a ID (by default i think). So just a simple lookup for values in second table.
So i read and tried and read, and tried.. edited the changeset function, my its a wrong place. no.. how i have to do this??? I have posted the code from the schema below. This Ecto (changeset, …) is a blackbox for me. really hard to get in my brain.
schema "parametersets" do
field :name, :string
has_many :parameter, ProberParameterfileAssembler.Parameter.Posts
timestamps()
end
end
defmodule ProberParameterfileAssembler.Parameter.Posts do
use Ecto.Schema
import Ecto.Changeset
schema "parameters" do
field :address, :integer
field :description, :string
field :value, :integer
timestamps()
belongs_to :parameterset, ProberParameterfileAssembler.Parameter.Parameterset
end
@doc false
def changeset(posts, attrs) do
posts
|> ProberParameterfileAssembler.Repo.preload(:parameterset)
|> cast(attrs, [:address, :value, :description, :parameterset])
|> cast_assoc(:parameterset)
|> validate_required([ :value, :description])
end
currently i am getting protocol Phoenix.HTML.Safe not implemented for #Ecto.Association.NotLoaded<association :parameterset is not loaded> of type Ecto.Association.NotLoaded (a struct) error.
Trending in Questions
Other Trending 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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
joshdcuneo
This error is likely coming from a template somewhere. When your
ProberParameterfileAssembler.Parameter.Postsstruct does not have the parameterset association loaded the value of the:parametersetkey on that struct would be anEcto.Association.NotLoadedstruct.If you look in your templates for something like
<%= @post.parameterset %>you can probably find the location where the error is raised.In order to display the parameterset you will need to load the association, possibly by using
Repo.preload/3which is documented here and then render the values.LostKobrakai
As a bit of additional context: Differently to other ORMs or db libraries in various languages ecto does not automatically load relationships. Those fields by default are populated with the
Ecto.Association.NotLoadedstruct and are only filled with real data once you explicitly load the associations usingpreload– giving you the tools to prevent n+1 queries from the start.twobeers
thanks for the hint… but as you can see.. this
parametersetitem is a part of the schema (struct?) or where should i have a look at ?and 2. i do something with preload inside the changeset above. but this do not help here…
sbuttgereit
I think this is the issue.
Repo.preload/3doesn’t appear to be something you would use in a Changeset at all. From the docs (emphasis mine):Changesets come before you get any populated structs back. As I read the docs for
preload/3the order of operations would be process the Changeset (with nopreload/3), call your insert or update using thereturning: trueoption (assuming PostgreSQL here), and then on the resulting struct of the insert/update callRepo.preload/3. So much after the Changeset.I haven’t tried this yet and I have needed
Repo.preload/3myself so my answer is just from reading the docs, so I could yet be wrong, but that’s my thinking on the issue.ADDENDUM:
If the struct you are passing to the Changeset was from a previous query, then I would think that would work here, but that’s not clear from the code you’ve included. If you’re starting with a struct that didn’t originate from database, then I would expect issues.
al2o3cr
The changeset uses
cast_assocto build a new value forparameterset, preloading in the changeset will not do what you want.You need to add preloading after the result of
Posts.changesetis sent to the database.dimitarvp
You don’t use
preloadin functions where nothing is loaded from the DB yet. Thechangesetfunction is for passing data to the schema and validating it.twobeers
Okay. Thanks to all your thoughts.
I will conclude to make sure i understand your right:
both
cast_assocandpreloadare not usually called in a changeset scope(?) I have to do it at another step.but where? I am not sure when this changeset function is invoked..
I have no idea about a structure here… sorry, this is to strange for me.
dimitarvp
cast_assocmust be used in changeset scope,preload– not.sbuttgereit
Everything I read in your posts suggest that you’re finding the role of Changesets confusing.
Changesets are all about validating that a set of proposed changes meet the criteria that you set. There’s a little bit more to it than that in that often times a Changeset will determine if there’s even a change at all being made as compared to a starting data struct you provide, but not much more than that. A Changeset is not really involved in questions of data retrieval. Preloads on the other hand are involved in data retrieval.
So, for example, in the Changeset you posted, the
validate_required/3call you made says that any proposed change that is validated by the Changeset must include values for:valueand:description. These values may come from the starting data or be part of the change you’re validating, but they must have a value. Thecast/4andcast_assoc/3calls are doing the initial comparison of thepostsargument (the original or starting data struct, the struct before the proposed changes) and theattrsargument which is the map containing the changes you want to make toposts. The casts produce an initial Changeset struct which has figured out what exactly is changing (if anything). Eventually the Changeset struct is passed on a function likeinsert/2orupdate/2and if all of the validations in the Changeset were passed, the insert or update takes place… if the Changeset failed in one of its requirements, then the insert/update doesn’t happen and you get a corresponding failure return result from those functions.preload/3on the other hand is about data retrieval. As others have mentioned, associated data is not automatically loaded in a query, you have to ask for it to be “preloaded”.Repo.preload/3is used after you have already retrieved data from a query but without preloaded associations at query time: you can ask for that data after the fact.Ecto.Query.preload/3allows you to ask for the preload in the query itself (seeEcto.Query.from/2). Changesets are not used in data retrieval sopreloaddoesn’t apply.Our answers may have been a bit confusing, too, because while Changesets aren’t involved in data retrieval, an insert or update can return a data struct if requested (and if using a database that supports it) via the
returning: trueoption. If you get data back from the insert or update that way, I would expect that you could then useRepo.preload/3to load the associations from that returned data.