英文:
Error 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key (using gorm and mysql)
问题
当我将gorm模型添加到我的结构体中时,我遇到了这个错误。
我在其他三个结构体中使用了gorm模型,但只有当我将它添加到我的journal结构体中时,才会出现错误 Error 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key(错误 1075:表定义不正确;只能有一个自动列,并且必须将其定义为键)。
以下是我的结构体:
package migrations
import "gorm.io/gorm"
type Category struct {
gorm.Model
Title string `gorm:"type:varchar(255)"`
Sort int `gorm:"sort"`
}
package migrations
import "gorm.io/gorm"
type Contents struct {
gorm.Model
CategoryId uint
CategoryModel Category `gorm:"foreignKey:category_id"`
Title string `gorm:"type:varchar(255)"`
Content string `gorm:"content"`
Status bool `gorm:"default:true"`
Sort int `gorm:"sort"`
Images []Image `gorm:"foreignKey:content_id"`
}
package migrations
import "gorm.io/gorm"
type Image struct {
gorm.Model
ContentId uint
ContentModel Contents `gorm:"foreignKey:content_id"`
Title string `gorm:"type:varchar(255)"`
File string
Sort int `gorm:"sort"`
}
//`gorm:"type:varchar(255)"`
package migrations
import "gorm.io/gorm"
type Journal struct {
gorm.Model
ModelName string `gorm:"ModelName"`
UpdatedBy string `gorm:"UpdatedBy"`
Row uint `gorm:"foreignKey:category_id"`
NewValue string `gorm:"newValue"`
OldValue string `gorm:"oldValue"`
Column string `gorm:"Column"`
}
英文:
when I add gorm model to my struct i got this error
im using gorm model in my other 3 struct but only when i add this to my journal struct i got error Error 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key
here are my structs
package migrations
import "gorm.io/gorm"
type Category struct {
gorm.Model
Title string `gorm:"type:varchar(255)"`
Sort int `gorm:"sort"`
}
package migrations
import "gorm.io/gorm"
type Contents struct {
gorm.Model
CategoryId uint
CategoryModel Category gorm:"foreignKey:category_id"
Title string gorm:"type:varchar(255)"
Content string gorm:"content"
Status bool gorm:"default:true"
Sort int gorm:"sort"
Images []Image gorm:"foreignKey:content_id"
}
package migrations
import "gorm.io/gorm"
type Image struct {
gorm.Model
ContentId uint
ContentModel Contents `gorm:"foreignKey:content_id"`
Title string `gorm:"type:varchar(255)"`
File string
Sort int `gorm:"sort"`
}
//`gorm:"type:varchar(255)"`
package migrations
import "gorm.io/gorm"
type Journal struct {
gorm.Model
ModelName string `gorm:"ModelName"`
UpdatedBy string `gorm:"UpdatedBy"`
Row uint `gorm:"foreignKey:category_id"`
NewValue string `gorm:"newValue"`
OldValue string `gorm:"oldValue"`
Column string `gorm:"Column"`
}
答案1
得分: 0
当gorm进行迁移时,通常会迁移添加到表中的内容。但是,如果你从结构体中删除了任何内容,在迁移过程中该内容不会从表中删除。假设结构体中有一个Key
列,类型为auto
。如果你从结构体中删除了它,在迁移后它不会自动从表中删除,而是需要手动从表中删除。我认为你可能遇到了这种情况。
英文:
When gorm do migration it usually migrates what is added to table. But if you remove any thing from struct that thing is not removed from table while migration. Suppose there was a Key
column of auto
type. If you remove it from struct it will not be auto removed from table after migration rather you need to manually remove it from table. I think this type of scenario happened with you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论