I am banging my head on the wall for some time as I cannot figure out why preload "category"association doesn’t work while all the others work just fine.
Need an eye of someone more experienced to point me in the right direction.
My Migrations
defmodule MyApp.Repo.Migrations.CreateCategories do
use Ecto.Migration
def change do
create table(:categories) do
add :name, :string
timestamps()
end
end
end
################
defmodule MyApp.Repo.Migrations.CreateQuestions do
use Ecto.Migration
def change do
create table(:questions) do
add :desc, :text
add :answers, {:array, :string}
add :user_id, references(:users)
add :category_id, references(:categories)
timestamps()
end
end
end
Schema
question.ex
defmodule MyApp.Questions.Question do
use Ecto.Schema
import Ecto.Changeset
alias MyApp.{User, Category}
schema "questions" do
field :answers, {:array, :string}
field :desc, :string
belongs_to :user, User
belongs_to :category, Category
timestamps()
end
@doc false
def changeset(question, attrs) do
question
|> cast(attrs, [:desc, :answers])
|> validate_required([:desc, :answers])
end
end
category.ex
defmodule MyApp.Categories.Category do
use Ecto.Schema
import Ecto.Changeset
alias MyApp.Questions.Question
schema "categories" do
field :name, :string
has_many :questions, Question
timestamps()
end
@doc false
def changeset(category, attrs) do
category
|> cast(attrs, [:name])
|> validate_required([:name])
end
end
If I do Repo.all(Question) |> Repo.preload(:category)
I get the below error
** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for MyApp.Category of type Atom, the given module does not exist. This protocol is implemented for the following type(s): BitString, Tuple, Ecto.Query, Atom, Ecto.SubQuery
It works fine when I preload questions through categories.