登录
首页 >  Golang >  Go问答

标题重写:唯一或排除约束与 ON CONFLICT 规范不匹配

来源:stackoverflow

时间:2024-02-18 11:24:24 435浏览 收藏

怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《标题重写:唯一或排除约束与 ON CONFLICT 规范不匹配》,涉及到,有需要的可以收藏一下

问题内容

我正在尝试对名为 feature_to_model 的表执行更新插入。但是,我收到以下错误:

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 都是索引),因为它们都不是唯一的


正确答案


错误:没有与 on conflict 规范相匹配的唯一约束或排除约束

这是因为 training_job_id 是 foreign key constraint 中引用的列,而不是索引。事实上,如果您希望速度更快,您可以考虑另外添加索引。来自 on conflict 的文档

你可能想要这样的东西,

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)

好了,本文到此结束,带大家了解了《标题重写:唯一或排除约束与 ON CONFLICT 规范不匹配》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>