简体   繁体   English

gorm 不生成字符串列

[英]gorm does not generate string columns

I'm trying to migrate my models to PostgreSQL database using gorm.我正在尝试使用 gorm 将我的模型迁移到 PostgreSQL 数据库。 But gorm does not generate string columns in tables.但是 gorm 不会在表中生成字符串列。 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 ).但是对于表用户,没有字符串列( usernamepassword )。 It has only gorm coumns (id, created_date, deleted_date, updated_date).它只有 gorm 列(id、created_date、deleted_date、updated_date)。

Is this a bug or am I doing something wrong?这是一个错误还是我做错了什么?

My go build is up to date.我的 go 版本是最新的。 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:来自 go.mod 的 Gorm 版本:

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:你会得到这样的表:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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