twobeers

twobeers

i am struggling with ecto these days… i really do not understand concepts :frowning:

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.

Showing Posts 1 to 9

joshdcuneo

joshdcuneo

This error is likely coming from a template somewhere. When your ProberParameterfileAssembler.Parameter.Posts struct does not have the parameterset association loaded the value of the :parameterset key on that struct would be an Ecto.Association.NotLoaded struct.

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/3 which is documented here and then render the values.

<%= @post.paremeterset.address %>
<%- @post.parameterset.description %>
LostKobrakai

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.NotLoaded struct and are only filled with real data once you explicitly load the associations using preload – giving you the tools to prevent n+1 queries from the start.

twobeers

twobeers OP

thanks for the hint… but as you can see.. this parameterset item 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

sbuttgereit

I think this is the issue. Repo.preload/3 doesn’t appear to be something you would use in a Changeset at all. From the docs (emphasis mine):

This is similar to Ecto.Query.preload/3 except it allows you to preload structs after they have been fetched from the database.

Changesets come before you get any populated structs back. As I read the docs for preload/3 the order of operations would be process the Changeset (with no preload/3), call your insert or update using the returning: true option (assuming PostgreSQL here), and then on the resulting struct of the insert/update call Repo.preload/3. So much after the Changeset.

I haven’t tried this yet and I have needed Repo.preload/3 myself 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

al2o3cr

The changeset uses cast_assoc to build a new value for parameterset, preloading in the changeset will not do what you want.

You need to add preloading after the result of Posts.changeset is sent to the database.

dimitarvp

dimitarvp

post_id_in_database
|> Repo.get()
|> Repo.preload(:parameterset)

You don’t use preload in functions where nothing is loaded from the DB yet. The changeset function is for passing data to the schema and validating it.

twobeers

twobeers OP

Okay. Thanks to all your thoughts.

I will conclude to make sure i understand your right:

both cast_assoc and preload are 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

dimitarvp

cast_assoc must be used in changeset scope, preload – not.

sbuttgereit

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/3 call you made says that any proposed change that is validated by the Changeset must include values for :value and :description. These values may come from the starting data or be part of the change you’re validating, but they must have a value. The cast/4 and cast_assoc/3 calls are doing the initial comparison of the posts argument (the original or starting data struct, the struct before the proposed changes) and the attrs argument which is the map containing the changes you want to make to posts. 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 like insert/2 or update/2 and 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/3 on 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/3 is 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/3 allows you to ask for the preload in the query itself (see Ecto.Query.from/2). Changesets are not used in data retrieval so preload doesn’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: true option. If you get data back from the insert or update that way, I would expect that you could then use Repo.preload/3 to load the associations from that returned data.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
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
samoloth
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
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews