elixir - 如何在 Ecto 迁移的执行语句中插入 Elixir 列表?
问题描述
如何执行此迁移:
types = ["a", "b", "c"]
execute "UPDATE table SET type='d' WHERE type in #{types}"
通常,您会执行以下操作:
execute "UPDATE table SET type='d' WHERE type in ('a', 'b', 'c')"
但是如果类型来自一个可变长度的列表呢?
解决方案
这是一种(可能是坏的)方法:
types = ["a", "b", "c"]
execute "UPDATE table SET type='d' WHERE type in ('#{Enum.join(types, "', '")}')"
这只是构建字符串。可能有更好的方法,涉及将函数传递给execute
,就像这样(来自文档):
execute(fn -> repo().query!("select 'Anonymous function query …';", [], [log: :info]) end)
https://hexdocs.pm/ecto_sql/Ecto.Migration.html#execute/1
这看起来像:
execute(fn -> repo().query!("update table set type='d' where type = any($1)", [types]) end)
推荐阅读
- javascript - 在select2下拉列表中动态设置maximumSelectionLength?
- python-3.x - 通过填充孔合并列表
- java - 在 gradle 同步时面临问题 - 找不到 lint-gradle-api.jar (com.android.tools.lint:lint-gradle-api:26.1.2)
- flutter - Flutter MapView 实例问题。第二次看不到标记
- asp.net - 启用 Postman 客户端缓存
- angular - 有没有办法从 Angular 6 Universal 的 server.ts 文件中的 interceptor.service.ts 访问变量?
- angular - 角度无法识别“napa”
- mysql - 如何使用 if check 更改多个表的列数据类型?
- scala - 结合scala宏注解
- java - 将 .txt Spark 输出转换为 .csv