【发布时间】:2021-06-18 18:52:31
【问题描述】:
我正在尝试对名为 feature_to_model 的表执行 upsert。但是,我收到以下错误:
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification (SQLSTATE 42P10)
这是我的桌子规格:
CREATE TABLE IF NOT EXISTS feature_to_model (
training_job_id varchar NOT NULL,
feature_name varchar NOT NULL,
feature_set_name varchar NOT NULL,
model_name varchar NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
PRIMARY KEY (training_job_id, feature_name, feature_set_name)
我使用 Gorm 来查询数据库,这是我的函数调用:
func (s *store) UpsertFeatureToModel(f2m *model.FeatureToModel) (*model.FeatureToModel, error) {
result := s.db.Table(f2mTable).Clauses(clause.OnConflict{
UpdateAll: true,
}).Create(f2m)
if result.Error != nil {
return nil, result.Error
}
return f2m, nil
}
我错过了什么?我不能对任何索引使用 UNIQUE 约束(training_job_id、feature_name、feature_set_name 是索引),因为它们都不是唯一的
【问题讨论】:
-
这 3 列是唯一的。您已将它们定义为主键,根据定义,它必须是唯一的。在本机 Postgres 中,您将指定 on conflict(training_job_id, feature_name, feature_set_name)。我不确定你是如何在你的模糊模型中做到这一点的,但这就是你所需要的。
标签: sql postgresql go go-gorm