【发布时间】:2020-10-08 06:05:24
【问题描述】:
我在 postgresql 中有一个表,我想用随机数更新所有记录的“点”列的值。在其他语言中,我们可以遍历所有 db 记录,但是我如何使用 ecto 来做到这一点?我试过这个:
Repo.all(from u in User, update: User.changeset(u, %{points: :rand.uniform(100)}))
但它输出以下错误:
== Compilation error in file lib/hello_remote/user.ex ==
** (Ecto.Query.CompileError) malformed update `User.changeset(u, %{points: :rand.uniform(100)})` in query expression, expected a keyword list with set/push/pop as keys with field-value pairs as values
expanding macro: Ecto.Query.update/3
lib/hello_remote/user.ex:30: HelloRemote.User.update_points/0
expanding macro: Ecto.Query.from/2
lib/hello_remote/user.ex:30: HelloRemote.User.update_points/0
(elixir 1.10.4) lib/kernel/parallel_compiler.ex:304: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
我也试过这个:
from(u in User)
|> Repo.update_all(set: [points: Enum.random(0..100)])
但它会更新所有具有相同值的记录
【问题讨论】:
标签: postgresql elixir ecto phoenix