从gorm模型创建主键时出现问题 [英] Issue while creating primary key from gorm model

查看:291
本文介绍了从gorm模型创建主键时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从gorm模型创建主键时,它返回错误重复的列名:"id""

While creating primary key from gorm model it return with error "duplicate column name: "id""

我的模型看起来像

type User struct { 
gorm.Model 
Id string gorm:"primary_key;" 
FirstName string 
LastName string 
}

不知道上述模型有什么问题

any idea what is the issue with above model

推荐答案

Gorm使用一部分正在嵌入.

Gorm uses ID as the primary key by default. It is part of the gorm.Model you are embedding.

在嵌入 gorm.Model 时,应保留 ID ,因为gorm已包含它.另一种方法是删除嵌入的 gorm.Model 并自己指定 ID .

When embedding the gorm.Model, you should leave ID out as gorm already includes it. The alternative is to remove the embedded gorm.Model and specify ID yourself.

引用浏览器惯例页面:

gorm.Model是一个基本的GoLang结构,其中包括以下内容字段:ID,CreatedAt,UpdatedAt和DeletedAt.

gorm.Model is a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt.

它可以嵌入到您的模型中,或者您可以构建自己的模型没有它.

It may be embeded into your model or you may build your own model without it.

在创建模式而不是在编译中失败的原因是,除非引用对象名称( Id id匹配",否则很多数据库(包括CockroachDB)都进行不区分大小写的检查.code>,但"Id" 则没有).与不区分大小写的结果相比,这将导致两个单独的列名称匹配.

The reasons this fails on schema creation as opposed to compilation is that a lot of databases (CockroachDB included) do case insensitive checking unless you quote the object names (Id matches id, but "Id" does not). This results in two separate column names that match when compared with case insensitivity.

这篇关于从gorm模型创建主键时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆