`[error] an exception was raised` (does not have association)

[debug] QUERY OK source="topics" db=2.9ms
SELECT t0."id", t0."title", t0."user_id" FROM "topics" AS t0 WHERE (t0."id" = $1) [4]

[error] an exception was raised:
    ** (ArgumentError) schema Discuss.Topic does not have association :comments
        (ecto 2.0.6) lib/ecto/association.ex:121: Ecto.Association.association_from_schema!/2
        (elixir 1.10.0) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
        (discuss 0.0.1) web/channels/comments_channel.ex:9: Discuss.CommentsChannel.join/3
        (phoenix 1.2.5) lib/phoenix/channel/server.ex:170: Phoenix.Channel.Server.init/1
        (stdlib 3.12) gen_server.erl:374: :gen_server.init_it/2
        (stdlib 3.12) gen_server.erl:342: :gen_server.init_it/6
        (stdlib 3.12) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
[info] Replied comments:4 :error

I’m doing a discussion topic, where you have the comments route, but when you click the add comment button, this error comes!

Hello and welcome,

Can You provide more information? Like this it’s difficult to answer…

In particular, the comments_channel, and the topic schema.

That might tell why You have this error message.

Please don’t forget to wrap code aroud ``` like in markdown to make it more readable.

defmodule Discuss.Comment do
  use Discuss.Web, :model

  @derive {Poison.Encoder, only: [:content, :user]}

  schema "comments" do
    field :content, :string
    belongs_to :user, Discuss.User
    belongs_to :topic, Discuss.Topic

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:content])
    |> validate_required([:content])
  end
end
defmodule Discuss.Topic do
  use Discuss.Web, :model

  schema "topics" do
    field :title, :string
    belongs_to :user, Discuss.User
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:title])
    |> validate_required([:title])
  end
end

It tells me You are using an old version of Phoenix, with models and poison.

Probably 1.3 or earlier.

And your topic does not define has many comments.

This is saying that you have a query where you are trying to load (or otherwise use) the comments filed, but Discus.Topic has no such association. Looking at your schema, Discuss.Topic has an association to Discuss.User, but not to comments.

You will need to add that in the schema for Discuss.Topic, something like: has_many: :comments, Discuss.Comments (assuming that is the name of it)

1 Like

Just a small typo, it should be has_many, not has-many :slight_smile:

wherps… missed the shift-key it seems :wink: thanks.

one solution was:
And your topic does not define has many comments.
The problem was not the version, it helped me a lot. Thanks!

defmodule Discuss.Comment do
  use Discuss.Web, :model

  @derive {Poison.Encoder, only: [:content, :user]}

  schema "comments" do
    field :content, :string
    belongs_to :user, Discuss.User
    belongs_to :topic, Discuss.Topic

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:content])
    |> validate_required([:content])
  end
end

defmodule Discuss.Topic do
  use Discuss.Web, :model

  schema "topics" do
    field :title, :string
    belongs_to :user, Discuss.User
    has_many :comments, Discuss.Comment
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:title])
    |> validate_required([:title])
  end
end

defmodule Discuss.User do
  use Discuss.Web, :model

  @derive {Poison.Encoder, only: [:email]}

  schema "users" do
    field :email, :string
    field :provider, :string
    field :token, :string
    has_many :topics, Discuss.Topic
    has_many :comments, Discuss.Comment

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:email, :provider, :token])
    |> validate_required([:email, :provider, :token])
  end
end

It is “so much”, NOT “so match”.

THANK YOU!

defmodule Discuss.Comment do
  use Discuss.Web, :model

  @derive {Poison.Encoder, only: [:content, :user]}

  schema "comments" do
    field :content, :string
    belongs_to :user, Discuss.User
    belongs_to :topic, Discuss.Topic

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:content])
    |> validate_required([:content])
  end
end
defmodule Discuss.Topic do
  use Discuss.Web, :model

  schema "topics" do
    field :title, :string
    belongs_to :user, Discuss.User
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:title])
    |> validate_required([:title])
  end
end