Error 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key (using gorm and mysql)

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

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:表定义不正确;只能有一个自动列,并且必须将其定义为键)。

以下是我的结构体:

  1. package migrations
  2. import "gorm.io/gorm"
  3. type Category struct {
  4. gorm.Model
  5. Title string `gorm:"type:varchar(255)"`
  6. Sort int `gorm:"sort"`
  7. }
  1. package migrations
  2. import "gorm.io/gorm"
  3. type Contents struct {
  4. gorm.Model
  5. CategoryId uint
  6. CategoryModel Category `gorm:"foreignKey:category_id"`
  7. Title string `gorm:"type:varchar(255)"`
  8. Content string `gorm:"content"`
  9. Status bool `gorm:"default:true"`
  10. Sort int `gorm:"sort"`
  11. Images []Image `gorm:"foreignKey:content_id"`
  12. }
  1. package migrations
  2. import "gorm.io/gorm"
  3. type Image struct {
  4. gorm.Model
  5. ContentId uint
  6. ContentModel Contents `gorm:"foreignKey:content_id"`
  7. Title string `gorm:"type:varchar(255)"`
  8. File string
  9. Sort int `gorm:"sort"`
  10. }
  11. //`gorm:"type:varchar(255)"`
  1. package migrations
  2. import "gorm.io/gorm"
  3. type Journal struct {
  4. gorm.Model
  5. ModelName string `gorm:"ModelName"`
  6. UpdatedBy string `gorm:"UpdatedBy"`
  7. Row uint `gorm:"foreignKey:category_id"`
  8. NewValue string `gorm:"newValue"`
  9. OldValue string `gorm:"oldValue"`
  10. Column string `gorm:"Column"`
  11. }
英文:

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

  1. package migrations
  2. import "gorm.io/gorm"
  3. type Category struct {
  4. gorm.Model
  5. Title string `gorm:"type:varchar(255)"`
  6. Sort int `gorm:"sort"`
  7. }

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"
}

  1. package migrations
  2. import "gorm.io/gorm"
  3. type Image struct {
  4. gorm.Model
  5. ContentId uint
  6. ContentModel Contents `gorm:"foreignKey:content_id"`
  7. Title string `gorm:"type:varchar(255)"`
  8. File string
  9. Sort int `gorm:"sort"`
  10. }
  11. //`gorm:"type:varchar(255)"`
  1. package migrations
  2. import "gorm.io/gorm"
  3. type Journal struct {
  4. gorm.Model
  5. ModelName string `gorm:"ModelName"`
  6. UpdatedBy string `gorm:"UpdatedBy"`
  7. Row uint `gorm:"foreignKey:category_id"`
  8. NewValue string `gorm:"newValue"`
  9. OldValue string `gorm:"oldValue"`
  10. Column string `gorm:"Column"`
  11. }

答案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.

huangapple
  • 本文由 发表于 2022年12月24日 14:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/74906008.html
匿名

发表评论

匿名网友

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

确定