postgresql - 子句.OnConflict() 在 UPSERT 期间不生成 where 条件
问题描述
我正在将 golang 与 gorm 一起使用并在 Postgres 上运行,并且需要执行 Upsert。任何帮助表示赞赏。
我正在插入“属性”表,并且在“id”冲突时,只有当“organization_id”与给定值匹配时,才应更新该行。
基于来自 https://gorm.io/docs/create.html#Upsert-On-Conflict和https://github.com/go-gorm/gorm/blob/master/clause/where_test.go的样本
result := tx.Table("attributes").Clauses(clause.OnConflict{
Columns: []clause.Column{{Table: "attributes", Name: "id"}},
Where: clause.Where{Exprs: []clause.Expression{clause.Eq{Column: "organization_id", Value: "2"}}},
UpdateAll: true,
}).Create(&attributes)
上面的代码生成以下没有 WHERE 子句的 SQL :
INSERT INTO "attributes" ("organization_id","name","id")
VALUES (2,"demo",15)
ON CONFLICT ("id") DO UPDATE SET "organization_id"="excluded"."organization_id", "name"="excluded"."name",
RETURNING "id"
带有 WHERE 子句的预期 SQL:
INSERT INTO "attributes" ("organization_id","name","id")
VALUES (2,"demo",15)
ON CONFLICT ("id") DO UPDATE SET "organization_id"="excluded"."organization_id", "name"="excluded"."name",
WHERE attributes.organization_id=2
RETURNING "id"
解决方案
推荐阅读
- assembly - 在屏幕上显示缓冲输入
- magento2 - 在 Magento 2.3 中覆盖/重写第三方模型不起作用
- amazon-redshift - Redshift 弹性调整大小——降级集群有多难?
- keras - 在这种情况下,哪个模型更好?
- ruby-on-rails - 是否可以/建议在没有 webpack(er) 的情况下运行 Rails 6?
- html - 如何使用 CSS(和 sass)使水平线垂直?
- r - 在 R 中执行分类变量的排列
- angular - Ionic 5、Angular 9 应用程序未在 Prod 构建中加载。但适用于离子发球。控制台没有错误
- r - 在已排序的数据框列表中查找元素的平均位置
- r - 使用 geom_text_repel 自定义在行尾显示的标签