- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
今天我遇到了一个相当有趣的问题。我正在尝试使用 Ecto 函数 Repo.insert_all/2
实现从 CSV 文件到数据库的批量插入,但是,有一件事困扰着我。
问题出在我的上下文中的以下代码:
defmodule AppName.Roles do
def bulk_insert(array_of_maps) do
try do
Repo.insert_all(Role, array_of_maps)
rescue
exception in Postgrex.Error ->
_handle_exception(exception) # or whatever
end
end
end
但就目前而言,这似乎是一个黑客。因为我知道有一个内置的变更集机制可以处理唯一的约束,但我不知道如何使用 Repo.insert_all/3
但是由于 insert_all
函数不关心变更集,这使得这变得更加困难。
(我当然指的是unique_constraint/2
)
我知道我可以:
使用 Multi
来执行此操作,但这会在后端创建单独的查询,而不是将其作为一个大查询来执行
使用 tryrescue
block 保留代码,但我想看看是否有更 Elixir-y 的方法来解决这个问题,因为模式匹配的原理,并让它崩溃术语。
最佳答案
Ecto.Repo.insert_all/3
接受选项列表,其中之一是
:on_conflict
— It may be one of:raise
(the default),:nothing
,:replace_all
,:replace_all_except_primary_key
,{:replace, fields}
, a keyword list of update instructions or anEcto.Query
query for updates.
通常,无论是否想要处理冲突,他们都会将该选项设置为有点帮助,而不是引发异常。
此外,:conflict_target
选项也可能用于提供不安全片段。
关于Elixir Repo.insert_all/2 和唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56284062/
所以我有一个对象数组,我想使用insert_all 保存它。我收到一条错误消息,指出我的某个字段不匹配。 edtr.json { "edtrs": [ { "dtr_date"
今天我遇到了一个相当有趣的问题。我正在尝试使用 Ecto 函数 Repo.insert_all/2 实现从 CSV 文件到数据库的批量插入,但是,有一件事困扰着我。 问题出在我的上下文中的以下代码:
今天我遇到了一个相当有趣的问题。我正在尝试使用 Ecto 函数 Repo.insert_all/2 实现从 CSV 文件到数据库的批量插入,但是,有一件事困扰着我。 问题出在我的上下文中的以下代码:
(请注意:这里是菜鸟,所以请 ELI5,谢谢。) 代码片段: defmodule Wallet do use Ecto.Repo, otp_app: :arbit, adapt
我是一名优秀的程序员,十分优秀!