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"`
}
CodePudding user response:
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.