Thanks for stopping by. I was hoping to get some help in better understanding how we are supposed to preload a field when doing a query since that is an error I’m running into.
The query I’m trying to run which is causing #Ecto.Association.NotLoaded<association :owner is not loaded>
and #Ecto.Association.NotLoaded<association :category is not loaded>
is
def products(user) do
from(p in Product, where: p.owner == ^user.id)
|> Repo.all()
|> Repo.preload(category: :products)
|> Repo.preload(owner: :products)
end
My schema tables look like this:
Products
schema "products" do
field(:description, :string)
field(:name, :string)
field(:price, :decimal)
belongs_to(:owner, Amoramor.Accounts.User)
belongs_to(:category, Amoramor.Shops.Category)
timestamps()
end
User
schema "users" do
field(:email, :string)
field(:is_admin, :boolean, default: false)
field(:password_hash, :string)
field(:password, :string, virtual: true)
has_many(:products, Amoramor.Shops.Product)
timestamps()
end
Category
schema "categories" do
field(:name, :string)
belongs_to(:owner, Amoramor.Accounts.User)
has_many(:products, Amoramor.Shops.Product)
timestamps()
end
Last my product schema when I migrated the table was
use Ecto.Migration
def change do
create table(:products) do
add(:name, :string, null: false)
add(:description, :string, null: false)
add(:price, :decimal, null: false)
add(:category_id, references(:categories, on_delete: :delete_all), null: false)
add(:owner_id, references(:users, on_delete: :delete_all), null: false)
timestamps()
end
Is there something I’m not understanding? Thank you in advance for explaining how we should approach association/preload.