Casper
Casper

Reputation: 313

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

Upvotes: 0

Views: 4957

Answers (3)

Reven Erlangga
Reven Erlangga

Reputation: 1

In my case, I using multiple databases and forget to declaration model from another database. So, when I declaration model, I solve the problem.

This is my code before :

func NewGormClient(c *conf.Data, logger log.Logger) *gorm.DB {
  db, err := gorm.Open(postgres.Open(c.Database.Source), &gorm.Config{})

  db.Use(dbresolver.Register(dbresolver.Config{
    Sources: []gorm.Dialector{
        postgres.Open(c.Database.Service.Common.Source),
    },
  }))
  if err != nil {
      log.Fatalf("failed opening connection to db: %v", err)
  }

  log.Infof("success connection to database")

  return db
}

Then I added model from another database, btw this model has relation with main database

func NewGormClient(c *conf.Data, logger log.Logger) *gorm.DB {
  db, err := gorm.Open(postgres.Open(c.Database.Source), &gorm.Config{})

  db.Use(dbresolver.Register(dbresolver.Config{
      Sources: []gorm.Dialector{
          postgres.Open(c.Database.Service.Common.Source),
      },
  }, model.Gender{}))

  if err != nil {
      log.Fatalf("failed opening connection to db: %v", err)
  }

  log.Infof("success connection to database")

  return db
}

I hope this helps you

Upvotes: 0

Ladking
Ladking

Reputation: 13

I also have similar error, I had to go through the gorm source code to see how migration was done. I noticed gorm sorts and reorder models according to constraint dependencies, which at times causes the dependent tables to be created before the table they depend on, which leads to the error.

"ERROR: relation does not exist (SQLSTATE 42P01)"

Suggested solution (but not the best) will be to create the table that has dependant first, then create the dependent tables.

Upvotes: 0

Casper
Casper

Reputation: 313

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!

Upvotes: 2

Related Questions