英文:
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"`
}
我真的不明白可能出了什么问题,因为我的Cart
、Jurik
和Phyz
表与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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论