按照 José Valim 本人的指示,我有一个带有嵌入式结构的 Elixir 结构:
http://blog.plataformatec.com.br/2015/08/working-with-ecto-associations-and-embeds/
defmodule MyApp.Post do
use Ecto.Schema
schema "posts" do
field :title
field :body
embeds_many :comments, MyApp.Comment
timestamps
end
end
defmodule MyApp.Comment do
use Ecto.Schema
embedded_schema "comments" do
field :body
belongs_to :post, MyApp.Post
timestamps
end
end
Run Code Online (Sandbox Code Playgroud)
除了嵌入评论的 updated_at 时间戳之外,几乎所有东西都可以完美运行,它在更新后似乎保留了它们的原始值。
我的意思是,如果我更改列表中的评论之一并在帖子上更新它并像这样保存:
my_post
|> change()
|> put_embed(:comments, comments)
|> Repo.update()
Run Code Online (Sandbox Code Playgroud)
对值的更新按预期进行,除了 Comment 的 updated_at 时间戳没有改变。
我可以手动设置:
|> Map.put(:updated_at, DateTime.to_iso8601(DateTime.utc_now))
Run Code Online (Sandbox Code Playgroud)
这有效,但试图弄清楚为什么它没有被 Ecto 自动设置。使用 Ecto 3.5.3。任何帮助,将不胜感激!
移民:
defmodule MyApp.Repo.Migrations.CreatePosts do
use Ecto.Migration
def change do
create table(:posts) do
add …Run Code Online (Sandbox Code Playgroud)