[英]ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
I'm trying to perform upsert against a table named feature_to_model.我正在尝试对名为 feature_to_model 的表执行更新插入。 However, I get the following error:但是,我收到以下错误:
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification (SQLSTATE 42P10)
Here's my table specifications:这是我的桌子规格:
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)
I use Gorm to query the db here's my function call for that:我使用 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
}
What am I missing?我错过了什么? I cannot use UNIQUE constraint on any of the indices (training_job_id, feature_name, feature_set_name are index), because none of them are unique我不能对任何索引使用 UNIQUE 约束(training_job_id、feature_name、feature_set_name 是索引),因为它们都不是唯一的
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification in错误:没有与 ON CONFLICT 规范匹配的唯一或排除约束
This because training_job_id is a column referenced in a FOREIGN KEY CONSTRAINT and not an index.这是因为 training_job_id 是在 FOREIGN KEY CONSTRAINT 中引用的列,而不是索引。 In fact, if you want that to be faster, you may consider additionally adding an index.事实上,如果你希望它更快,你可以考虑额外添加一个索引。 From the docs on ON CONFLICT来自 ON CONFLICT 的文档
You probably want something like this,你可能想要这样的东西,
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)
CONSTRAINT UC_feature_to_model UNIQUE (training_job_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.