carlomunguia

carlomunguia

Programming Phoenix LiveView: pg 222(epub) invalid association `product` in schema Pento.Survey.Rating: associated schema Product does not exist lib/pento/survey/rating.ex:1: Pento.Survey.Rating (module)

(Phoenix 1.6.6, Ecto 3.7.2, Elixir 1.13)
When building the custom Survey context, we are told to then update the rating.ex file with the following:

  schema "ratings" do
    field :stars, :integer
    belongs_to :user, User
    belongs_to :product, Product
    timestamps()
  end

then, to update our changeset

  def changeset(rating, attrs) do
    rating
    |> cast(attrs, [:stars, :user_id, :product_id])
    |> validate_required([:stars, :user_id, :product_id])
    |> validate_inclusion(:stars, 1..5)
    |> unique_constraint(:product_id, name: :index_ratings_on_user_product)
  end

After, we then update demographic.ex with similar changes, we are told to mix.ecto.migrate - but I get this error:

Compiling 2 files (.ex)
warning: invalid association `product` in schema Pento.Survey.Rating: associated schema Product does not exist
  lib/pento/survey/rating.ex:1: Pento.Survey.Rating (module)

However, the requisite migration files etc. are still updated.
Creating a Survey rating in IEX works but shows these curious AssociationNotLoaded entries for Product & User:

 %Pento.Survey.Rating{
   __meta__: #Ecto.Schema.Metadata<:loaded, "ratings">,
   id: 1,
   inserted_at: ~N[2022-04-13 18:57:09],
   product: #Ecto.Association.NotLoaded<association :product is not loaded>,
   product_id: 1,
   stars: 5,
   updated_at: ~N[2022-04-13 18:57:09],
   user: #Ecto.Association.NotLoaded<association :user is not loaded>,
   user_id: 1
 }}

All seems fine until you create the the query.ex for Demographic & then spin it in IEX , which never finds the “user function” (Survey.get_demographic_by_user).

iex(2)> Survey.get_demographic_by_user(user)
** (CompileError) iex:2: undefined function user/0 (there is no such import)

If you try to proceed further anyways by use the Catalog.list_products_with_user_rating function in IEX, we get this error:

Catalog.list_products_with_user_rating(user)
** (UndefinedFunctionError) function Pento.Survey.Rating.Query.preload_user/1 is undefined (module Pento.Survey.Rating.Query is not available)
    Pento.Survey.Rating.Query.preload_user(#Pento.Accounts.User<__meta__: #Ecto.Schema.Metadata<:loaded, "users">, confirmed_at: nil, email: "mercutio@grox.io", id: 1, inserted_at: ~N[2022-04-08 22:24:37], updated_at: ~N[2022-04-08 22:24:37], username: nil, ...>)
    lib/pento/catalog/product/query.ex:15: Pento.Catalog.Product.Query.preload_user_ratings/2
    (pento 0.1.0) lib/pento/catalog.ex:106: Pento.Catalog.list_products_with_user_rating/1

It seems that the User & Product schemas relating to the belong_to are not being recognized in the Rating schema (?)

Is there a piece to this puzzle I’m missing? Everything up until this point in the book has gone off without a hitch with no errors & the Product & User schemas definitely exist.

I even tried switching the ordering of product & user in the belong_to in Rating schema to see if that helped, but got the same error but in reverse:

Compiling 2 files (.ex)
warning: invalid association `user` in schema Pento.Survey.Rating: associated schema User does not exist
  lib/pento/survey/rating.ex:1: Pento.Survey.Rating (module)

Any help to move past this would be great, as I am currently roadblocked - otherwise an excellent book! Thank you

Marked As Solved

carlomunguia

carlomunguia

UPDATE: This was fixed by:

  • Deleting ratings table & rating.ex file

  • Re-running context gen command for Rating

  • Updating the schema in rating.ex to the following:

belongs_to: :user, Pento.Accounts.User
belongs_to: :product, Pento.Catalog.Product
  • Update changeset along with the book

  • Re-run mix ecto.migrate

  • Keep all other files i.e. demographics etc. the same

Now all associations are loaded in survey/query & product/query correctly.
I’m not sure why the association has to be explicit in this case, but I’m finally to be free from the roadblock - FYI if you happen to see anything like this in future, Sophie/Bruce and all other readers.

Where Next?

Popular Pragmatic Bookshelf topics Top

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
iPaul
page 37 ANTLRInputStream input = new ANTLRInputStream(is); as of ANTLR 4 .8 should be: CharStream stream = CharStreams.fromStream(i...
New
johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
gorkaio
root_layout: {PentoWeb.LayoutView, :root}, This results in the following following error: no “root” html template defined for PentoWeb...
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
New

Sub Categories: