Need help understanding table relations in Ecto

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.

1 Like

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 %>
2 Likes

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.

3 Likes

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…

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.

1 Like

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.

1 Like
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.

1 Like

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.

1 Like

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

1 Like

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.

4 Likes