简体   繁体   English

错误:没有与 ON CONFLICT 规范匹配的唯一或排除约束

[英]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.

相关问题 错误:pq:没有与 ON CONFLICT 规范匹配的唯一或排除约束 - ERROR: pq: there is no unique or exclusion constraint matching the ON CONFLICT specification 没有与 ON CONFLICT 匹配的唯一或排除约束 - No unique or exclusion constraint matching the ON CONFLICT Postgers:使用主键和 UNIQUE 列时,没有唯一或排除约束匹配 ON CONFLICT 规范 - Postgers: No unique or exclusion constraint matching ON CONFLICT specification when using a primary key and a UNIQUE column postgres 插入冲突错误 - 没有唯一或排除约束 - postgres insert on conflict error - there is no unique or exclusion constraint 错误:具有FK时没有唯一约束匹配 - ERROR: No unique constraint matching when having FK 错误:没有唯一约束匹配给定表的键 - ERROR: No unique constraint matching given keys for referenced table 如何修复错误“没有唯一约束匹配引用表的给定键” - How to fix error "there is no unique constraint matching given keys for referenced table" "错误:没有唯一约束匹配引用表“事件”的给定键" - error: there is no unique constraint matching given keys for referenced table "incident" 无法解释此错误:没有与引用表的给定键匹配的唯一约束 - Unable to explain this error: there is no unique constraint matching given keys for referenced table Postgresql错误:没有唯一约束匹配给定键的引用表 - Postgresql ERROR: there is no unique constraint matching given keys for referenced table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM