[英]Gorm not reading columns on select
I'm running a SELECT using WHERE on gorm but the results are comming with the columns store_name and type empty("").我在 gorm 上使用 WHERE 运行 SELECT,但结果与列 store_name 和类型 empty("") 一起出现。 I have the following struct on Go:我在 Go 上有以下结构:
type Store struct {
ID uint
StoreName string
Type string
Code string `gorm:"unique"`
Active bool
CreatedAt *time.Time
UpdatedAt *time.Time
}
The following database on Postgres: Postgres 上的以下数据库:
CREATE TABLE IF NOT EXISTS stores
(
id BIGSERIAL PRIMARY KEY,
store_name TEXT NOT NULL,
type TEXT NOT NULL,
code TEXT NOT NULL UNIQUE,
active BOOLEAN DEFAULT true,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ
);
Im running this select right here:我在这里运行这个 select:
var store Store
result := db.First(&stores).Where("code = ?", code)
if err := result.Error; err != nil {
return nil, err
}
return &stores, nil
Does anyone know what I may be doing wrong?有谁知道我可能做错了什么? All columns are returned on the SELECT except for the columns StoreName and Type. SELECT 上返回除列 StoreName 和 Type 之外的所有列。 Thank you so much in advance!非常感谢您!
You need to set the field tag if field name differs from DB column name.如果字段名称与数据库列名称不同,则需要设置字段标签。
Example:例子:
StoreName string `gorm:"column:store_name"`
See this document: https://gorm.io/docs/models.html请参阅此文档: https://gorm.io/docs/models.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.