Elixir/Ecto 无法识别模型中的外键
Elixir/Ecto not recognizing Foreign Key in Model
我正在尝试在 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,并且在我的其余代码中调用了 article_id .
这是我调用 Repo 的地方
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)
我完全迷路了。有人有什么想法吗?
由于您首先获取文章然后获取其评论,因此 Ecto 将使用 Articles
模型的 has_many
声明,您很可能会错过自定义 foreign_key
在那。
has_many :comments, Newsly.Comments, foreign_key: :article_id
^^^^^^^^^^^^^^^^^^^^^^^^
我正在尝试在 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,并且在我的其余代码中调用了 article_id .
这是我调用 Repo 的地方
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)
我完全迷路了。有人有什么想法吗?
由于您首先获取文章然后获取其评论,因此 Ecto 将使用 Articles
模型的 has_many
声明,您很可能会错过自定义 foreign_key
在那。
has_many :comments, Newsly.Comments, foreign_key: :article_id
^^^^^^^^^^^^^^^^^^^^^^^^