1
我有一个博客应用程序,当有人更新帖子时,它会将一个条目添加到newsfeed表中。该Post
和Newsfeed
模式如下:使用Ecto.Multi更新记录时遇到问题
mix phx.new.json Content Post posts title:string content:string
mix phx.new.json Content Newsfeed newsfeeds message:string
下面是包装函数:
def updateContent(%{id: id, content: content}, _info) do
post = Repo.get(post, id)
Content.update_content_and_add_to_newsfeed(post, %{id: id, content: content})
end
这里是在内容方面的逻辑:
def update_content_and_add_to_newsfeed(post, %{id: id, content: content}) do
multi =
Multi.new
|> Multi.update(:post, update_post(post, %{content: content}))
|> Multi.insert(:newsfeed, %Newsfeed{message: "post updated"})
case Repo.transaction(multi) do
{:ok, %{post: post}} ->
{:ok, post}
{:error, _} ->
{:error, "Error"}
end
end
这里是update_post功能:
def update_post(%Post{} = post, attrs) do
post
|> Post.changeset(attrs)
|> Repo.update()
end
当我运行这段代码,在数据库中的内容更新,但没有新闻源项目被插入,我在控制台中看到此错误消息:
Server: localhost:4000 (http)
Request: POST /graphiql
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in Ecto.Multi.update/4
任何想法如何解决这一问题?我正在使用ecto v2.2.6和phoenix 1.3和Absinthe
。
这可能需要你使用'使用{:错误,_}'而不是'{:错误,_,_,_}'。你也可以发布你的'update_post'方法的代码吗? – Sheharyar
查看上面的更新 –