Home > Software design >  gorm does not generate string columns
gorm does not generate string columns

Time:12-09

I'm trying to migrate my models to PostgreSQL database using gorm. But gorm does not generate string columns in tables. Here are my models:

type Task struct {
   gorm.Model
   Answer    float64
   TaskParts []TaskPart `gorm:"foreignKey:TaskId"`
}

type TaskPart struct {
   gorm.Model
   Answer float64
   Items  []BackpackTaskItem `gorm:"foreignKey:TaskPartId"`
   TaskId uint
}

type BackpackTaskItem struct {
   gorm.Model
   Weight     float64
   Price      float64
   IsFixed    bool
   TaskPartId uint
}

type TaskUserSolution struct {
   gorm.Model
   TaskPartId uint
   TaskPart   TaskPart `gorm:"foreignKey:TaskPartId;references:ID"`
   UserId     uint
   User       User `gorm:"foreignKey:UserId;references:ID"`
}

type User struct {
   gorm.Model
   username string
   password string
}

Then I run this line of code:

err = db.AutoMigrate(&Task{}, &TaskPart{}, &BackpackTaskItem{}, &TaskUserSolution{}, &User{})

It does not give me any errors and successfully creates tables. But for table users there are no string columns (username and password). It has only gorm coumns (id, created_date, deleted_date, updated_date).

Is this a bug or am I doing something wrong?

My go build is up to date. I tried adding string fields to other models, but it did not create string columns either.

I completely dropped tables, but it did not help.

Gorm versions from go.mod:

gorm.io/driver/postgres v1.4.5
gorm.io/gorm v1.24.2

CodePudding user response:

Using lowercase latter at the beginning of the variable name makes it private, Use uppercase at the beginning of the variable name at the time of declaration

Like this :

 type User struct {
    gorm.Model
    Username string 
    Password string 
 }

you will get table like this :

enter image description here

  • Related