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.

Popular Pragmatic topics Top

belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
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
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
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
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
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
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New

Latest in PragProg

View all threads ❯