Search code examples
gogo-gormgo-gin

Relation does not exist GORM


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


Solution

  • 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!