如何使用 Ecto 和 Postgres 插入或更新具有不同值的多行?
如果我有一个架构/结构:%Counter{key: String.t(), count: integer()}
如何插入或更新多个条目?如果记录不存在,我想插入它,但如果它存在,我想增加该值。
[
%{key: "questions:asked", count: 1},
%{key: "questions:answered", count: 1},
%{key: "ads:viewed", count: 3}
]
Ecto.Repo.insert_all
和 :on_replace 看起来应该可以工作,但我希望每行都有唯一的值。
最佳答案
您可以使用 Ecto.Repo.insert_all
,但您必须提供查询并利用冲突操作中可用的 Postgresql 的 excluded
表。
upsert_query =
Counter
|> where([o], o.key == fragment("EXCLUDED.key"))
|> update(inc: [count: fragment("EXCLUDED.count")])
Repo.insert_all(Counter, records,
on_conflict: upsert_query,
conflict_target: [:key],
returning: false
)
排除的值是您传入的值,表示为仅在冲突时可用的临时表。
应该注意的是,如果您希望设置特定值,则可以将其与 set 一起使用,而不是 inc 。
有更好的解决方案吗?
关于postgresql - 如何使用 ecto 更新或插入多行? (更新插入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57376729/