问题描述
从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使用 ID
作为主键.它是 gorm.Model
的一部分正在嵌入.
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.
引用浏览器惯例页面:
它可以嵌入到您的模型中,或者您可以构建自己的模型没有它.
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模型创建主键时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!