英文:
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"
}
我想在我的ID字段上使用自动递增,但是当我执行AutoMigrate()时,没有任何变化。
我看到了这个链接,但是我不想使用gorm.Model,我想使用自己的ID字段。
下面的图片是我想要的效果:
英文:
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"
}
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..
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论