关系不存在 GORM

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

Relation does not exist GORM

问题

一切都正常工作,但当我清空数据库并重新启动应用程序时,出现以下错误:ERROR: relation "orders" does not exist (SQLSTATE 42P01)

我的代码:

  1. type Cart struct {
  2. gorm.Model
  3. Products JSONB `gorm:"type:jsonb" json:"products"`
  4. OrderID uint
  5. }
  6. type Jurik struct {
  7. gorm.Model
  8. Inn string `json:"inn" gorm:column:"inn"`
  9. ...
  10. OrderID uint
  11. }
  12. type Phyz struct {
  13. gorm.Model
  14. Name string `json:"name" gorm:column:"name"`
  15. ...
  16. OrderID uint
  17. }
  18. type Order struct {
  19. gorm.Model
  20. Cart Cart `json:"cart"`
  21. User_id string `json:"user_id"`
  22. Jurik Jurik `json:"jurik"`
  23. Phyz Phyz `json:"phyz"`
  24. }

我真的不明白可能出了什么问题,因为我的CartJurikPhyz表与Order相关联。

英文:

Everything worked, but when I cleared the database and started the application again, I get this error: ERROR: relation "orders" does not exist (SQLSTATE 42P01)

My code:

  1. type Cart struct {
  2. gorm.Model
  3. Products JSONB `gorm:"type:jsonb" json:"products"`
  4. OrderID uint
  5. }
  6. type Jurik struct {
  7. gorm.Model
  8. Inn string `json:"inn" gorm:column:"inn"`
  9. ...
  10. OrderID uint
  11. }
  12. type Phyz struct {
  13. gorm.Model
  14. Name string `json:"name" gorm:column:"name"`
  15. ...
  16. OrderID uint
  17. }
  18. type Order struct {
  19. gorm.Model
  20. Cart Cart `json:"cart"`
  21. User_id string `json:"user_id"`
  22. Jurik Jurik `json:"jurik"`
  23. Phyz Phyz `json:"phyz"`
  24. }

I really don't understand what could be wrong, because my Cart Jurik Phyz tables are related to Order

答案1

得分: 1

我不知道它是如何工作的,但是我之前的代码看起来是这样的:

  1. func Connect() {
  2. db, err := gorm.Open(postgres.Open("postgres://postgres:qwerty@localhost:5432/shop"), &gorm.Config{})
  3. if err != nil {
  4. panic(err)
  5. }
  6. db.AutoMigrate(&models.Categories{}, &models.Products{}, &models.Pagination{}, &models.Feedback{}, &models.Cart{}, &models.Jurik{}, &models.Phyz{}, &models.Order{})
  7. DB = db
  8. }

然后我尝试为Order创建一个单独的迁移:

  1. func Connect() {
  2. db, err := gorm.Open(postgres.Open("postgres://postgres:qwerty@localhost:5432/shop"), &gorm.Config{})
  3. if err != nil {
  4. panic(err)
  5. }
  6. db.AutoMigrate(&models.Categories{}, &models.Products{}, &models.Pagination{}, &models.Feedback{}, &models.Cart{}, &models.Jurik{}, &models.Phyz{})
  7. db.AutoMigrate(&models.Order{})
  8. DB = db
  9. }

希望这对某人有所帮助!

英文:

I don't know how it works, but my code before looked like this:

  1. func Connect() {
  2. db, err := gorm.Open(postgres.Open("postgres://postgres:qwerty@localhost:5432/shop"), &gorm.Config{})
  3. if err != nil {
  4. panic(err)
  5. }
  6. db.AutoMigrate(&models.Categories{}, &models.Products{}, &models.Pagination{}, &models.Feedback{}, &models.Cart{}, &models.Jurik{}, &models.Phyz{}, &models.Order{})
  7. DB = db
  8. }

Then I tried to make a separate migration for Order:

  1. func Connect() {
  2. db, err := gorm.Open(postgres.Open("postgres://postgres:qwerty@localhost:5432/shop"), &gorm.Config{})
  3. if err != nil {
  4. panic(err)
  5. }
  6. db.AutoMigrate(&models.Categories{}, &models.Products{}, &models.Pagination{}, &models.Feedback{}, &models.Cart{}, &models.Jurik{}, &models.Phyz{})
  7. db.AutoMigrate(&models.Order{})
  8. DB = db
  9. }

Hope this helps someone!

huangapple
  • 本文由 发表于 2022年7月18日 00:10:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/73013374.html
匿名

发表评论

匿名网友

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

确定