GORM和SQLite:自增不起作用

huangapple go评论88阅读模式
英文:

GORM and SQLite : auto increment don't work

问题

我有以下的结构体:

// Equipement
type Equipement struct {
   ID int gorm:"primaryKey;autoIncrement"
   IP string gorm:"not null"
   Username string gorm:"not null"
   AuthPass string gorm:"not null"
   AuthProtocol string gorm:"not null"
   PrivatePass string gorm:"not null"
   PrivateProtocol string gorm:"not null"
}

GORM和SQLite:自增不起作用

我想在我的ID字段上使用自动递增,但是当我执行AutoMigrate()时,没有任何变化。

我看到了这个链接,但是我不想使用gorm.Model,我想使用自己的ID字段。

下面的图片是我想要的效果:

GORM和SQLite:自增不起作用

英文:

I have this following struct :

// Equipement
type Equipement struct {
   ID int gorm:"primaryKey;autoIncrement"
   IP string gorm:"not null"
   Username string gorm:"not null"
   AuthPass string gorm:"not null"
   AuthProtocol string gorm:"not null"
   PrivatePass string gorm:"not null"
   PrivateProtocol string gorm:"not null"
}

GORM和SQLite:自增不起作用

and I want to put auto increment on my ID.. however I don't have anything when I do AutoMigrate()..

I see this link but I don't want to use gorm.Model and I want use my own ID.

Next image is what I want..

GORM和SQLite:自增不起作用

答案1

得分: 1

我理解AutoIncrement是在设置PrimaryKey时的默认选项。
尝试将autoIncrement更改为非空。

 ID             uint   `gorm:"primaryKey; not null"`

https://gorm.io/docs/composite_primary_key.html#content-inner

英文:

I understand AutoIncrement it is default when you set PrimaryKey.
Try change autoIncrement to not null.

 ID             uint   `gorm:"primaryKey; not null"`

https://gorm.io/docs/composite_primary_key.html#content-inner

huangapple
  • 本文由 发表于 2021年10月6日 17:16:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/69462943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定