简体   繁体   中英

gorm does not generate string columns

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

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM