关系不存在 GORM

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

Relation does not exist GORM

问题

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

我的代码:

type Cart struct {
	gorm.Model
	Products JSONB `gorm:"type:jsonb" json:"products"`
	OrderID  uint
}
type Jurik struct {
	gorm.Model
	Inn             string `json:"inn" gorm:column:"inn"`
	...
	OrderID         uint
}
type Phyz struct {
	gorm.Model
	Name    string `json:"name" gorm:column:"name"`
	...
	OrderID uint
}
type Order struct {
	gorm.Model
	Cart    Cart   `json:"cart"`
	User_id string `json:"user_id"`
	Jurik Jurik `json:"jurik"`
	Phyz  Phyz  `json:"phyz"`
}

我真的不明白可能出了什么问题,因为我的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:

type Cart struct {
	gorm.Model
	Products JSONB `gorm:"type:jsonb" json:"products"`
	OrderID  uint
}
type Jurik struct {
	gorm.Model
	Inn             string `json:"inn" gorm:column:"inn"`
	...
	OrderID         uint
}
type Phyz struct {
	gorm.Model
	Name    string `json:"name" gorm:column:"name"`
	...
	OrderID uint
}
type Order struct {
	gorm.Model
	Cart    Cart   `json:"cart"`
	User_id string `json:"user_id"`
	Jurik Jurik `json:"jurik"`
	Phyz  Phyz  `json:"phyz"`
}

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

答案1

得分: 1

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

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

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

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

希望这对某人有所帮助!

英文:

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

func Connect() {

	db, err := gorm.Open(postgres.Open("postgres://postgres:qwerty@localhost:5432/shop"), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	db.AutoMigrate(&models.Categories{}, &models.Products{}, &models.Pagination{}, &models.Feedback{}, &models.Cart{}, &models.Jurik{}, &models.Phyz{}, &models.Order{})
	DB = db
}

Then I tried to make a separate migration for Order:

func Connect() {

	db, err := gorm.Open(postgres.Open("postgres://postgres:qwerty@localhost:5432/shop"), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	db.AutoMigrate(&models.Categories{}, &models.Products{}, &models.Pagination{}, &models.Feedback{}, &models.Cart{}, &models.Jurik{}, &models.Phyz{})
	db.AutoMigrate(&models.Order{})
	DB = db
}

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:

确定