![](/img/trans.png)
[英]MySQL - 1075 Incorrect table definition, there can be only one auto column and it must be defined as a key
[英]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 模型添加到我的结构时,出现了这个错误
我在我的其他 3 个结构中使用 gorm 模型,但只有当我将其添加到我的日志结构时,我才收到错误错误 1075:不正确的表定义; 只能有一个自动列,它必须被定义为一个键这里是我的结构
package migrations
import "gorm.io/gorm"
type Category struct {
gorm.Model
Title string `gorm:"type:varchar(255)"`
Sort int `gorm:"sort"`
}
包迁移
导入“gorm.io/gorm”
type Contents struct { gorm.Model CategoryId uint CategoryModel Category gorm:"foreignKey:category_id"
标题字符串gorm:"type:varchar(255)"
内容字符串gorm:"content"
Status bool gorm:"default:true"
排序 int gorm:"sort"
图像 []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"`
}
当 gorm 进行迁移时,它通常会迁移添加到表中的内容。 但是,如果您从结构中删除任何东西,那么在迁移时该东西不会从表中删除。 假设有一个auto
类型的Key
列。 如果您将其从结构中删除,迁移后它不会自动从表中删除,您需要手动将其从表中删除。 我认为这种情况发生在你身上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.