我正在尝试以一对多关系检索和打印项目(在这种情况下,我想打印一篇文章及其所有评论),在elixir / ecto中。
我收到以下错误 -
[error] GenServer #PID<0.398.0> terminating
** (Ecto.QueryError) deps/ecto/lib/ecto/association.ex:516: field `articles_id` in `where` does not exist in schema Newsly.Comments in query:
from c in Newsly.Comments,
where: c.articles_id == ^1,
order_by: [asc: c.articles_id],
select: {c.articles_id, c}
(elixir) lib/enum.ex:1826: Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/enum.ex:1372: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
(elixir) lib/enum.ex:1826: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/repo/queryable.ex:124: Ecto.Repo.Queryable.execute/5
(ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4
(elixir) lib/enum.ex:1270: Enum."-map/2-lists^map/1-0-"/2
Last message: %Phoenix.Socket.Message{event: "addComment", join_ref: "43", payload: %{"articleid" => 1, "message" => "asdfasdfsasdfasdferqweasdfas", "user" => "peter"}, ref: "45", topic: "room:lobby"}
State: %Phoenix.Socket{assigns: %{}, channel: Newsly.RoomChannel, channel_pid: #PID<0.398.0>, endpoint: Newsly.Endpoint, handler: Newsly.UserSocket, id: nil, join_ref: "43", joined: true, private: %{log_handle_in: :debug, log_join: :info}, pubsub_server: Newsly.PubSub, ref: nil, serializer: Phoenix.Transports.V2.WebSocketSerializer, topic: "room:lobby", transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.389.0>, vsn: "2.0.0"}
这个错误的奇怪之处在于它说它无法找到articles_id
,但是当我进行注释的预加载以及我的其余代码时,我正在调用article_id。
这是我称之为回购的地方
defmodule Newsly.CommentController do
alias Newsly.{Repo, Articles, Comments}
def addComment(articleid, message, user) do
IO.puts "inside addComment() in CommentController"
article = Repo.get(Articles, articleid)
|> Repo.preload(:comments) #this line is giving the error - when I take it out the error goes away (although comments are not preloaded)
IO.puts "article"
IO.inspect article
end
end
这是我的模特 -
defmodule Newsly.Comments do
use Newsly.Web, :model
schema "comment" do
field :body, :string
field :user, :string
field :upvotes, :integer
field :downvotes, :integer
field :flaggedcount, :integer
belongs_to :article, Newsly.Articles, foreign_key: :article_id #I don't call articles_id here!!! only article_id (singular). Ecto is seeing in the database a relationship that does not exist!
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:body, :user, :upvotes, :downvotes, :flaggedcount])
|> validate_required([:body, :user, :upvotes, :downvotes, :flaggedcount])
end
end
只是为了双重检查......
这是我本地的postgres数据库关联 - 注意article_id存在(不是articles_id)
newsly_dev=# table comment;
id | body | user | upvotes | downvotes | flaggedcount | article_id | inserted_at | updated_at
----+------+------+---------+-----------+--------------+------------+-------------+------------
(0 rows)
我完全迷失了。有人有什么想法吗?
答案 0 :(得分:2)
由于您首先提取文章然后提取其评论,因此Ecto将使用Articles
模型的foreign_key
声明,您很可能错过了自定义{{1在那。
has_many :comments, Newsly.Comments, foreign_key: :article_id
^^^^^^^^^^^^^^^^^^^^^^^^