Gorm忽略了select语句中的列
时间:2024-03-26
浏览:174
作者:佚名
使用 Gorm 运行包含 `where` 子句的 `select` 语句时,如果字段名称与数据库列名称不同,需要在字段上设置 `gorm` 标签。如果不设置标签,则 Gorm 将忽略 `select` 语句中与未标记字段相对应的列。
问题内容
我正在 gorm 上使用 where 运行 select,但结果与列 store_name 和类型为empty("") 相关。我在 go 上有以下结构:
type store struct {
id uint
storename string
type string
code string `gorm:"unique"`
active bool
createdat *time.time
updatedat *time.time
}
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
);
我正在此处运行此选择:
var store Store
result := db.First(&stores).Where("code = ?", code)
if err := result.Error; err != nil {
return nil, err
}
return &stores, nil
有谁知道我做错了什么?除 storename 和 type 列外,所有列均在 select 中返回。提前非常感谢您!
正确答案
如果字段名称与数据库列名称不同,则需要设置字段标签。
示例:
StoreName string `gorm:"column:store_name"`
查看此文档:https://gorm.io/docs/models.html
好了,本文到此结束,带大家了解了《Gorm忽略了select语句中的列》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
