首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gorm ()查询,没有填充在定义的结构中定义的所有字段,以保存结果集

Gorm ()查询,没有填充在定义的结构中定义的所有字段,以保存结果集
EN

Stack Overflow用户
提问于 2022-03-14 14:10:27
回答 1查看 275关注 0票数 0

我遇到的问题是,结果集中返回的tx没有所有字段集。不知道为什么吗?它在实际数据库表中有值。

代码语言:javascript
复制
// request object supplied in FetchTransactionWithHighestExpiry:
type ProvisionRequest struct {
        Identity string     `json:"identity"`
        IdentityType string `json:"identityType"`
}

// struct to model database table
type Transactions struct {
        Identity string `gorm:"index;not null"`
        IdentityType string 
}

// method where there is problem
func (c *DBClient) FetchTransactionWithHighestExpiry(req *model.ProvisionRequest) (model.Transactions, error)  {
    var tx model.Transactions
    res := c.client.Model(&model.Transactions{}).Where("identity = ? AND endDate  > ?", req.Identity, time.Now().Unix()).Order("endDate desc").First(&tx)
    return tx, res.Error
}

我的tx只有标识集的值,而identityType是空字符串。知道我在这里做错什么了吗?

编辑1:事务表模式

代码语言:javascript
复制
-- rp.transactions definition

CREATE TABLE `transactions` (
  `transaction_id` varchar(255) NOT NULL,
  `identity` varchar(255) DEFAULT NULL,
  `identityType` varchar(15) DEFAULT NULL,
  `serviceId` varchar(255) DEFAULT NULL,
  `partnerId` varchar(255) DEFAULT NULL,
  `countryId` varchar(255) DEFAULT NULL,
  `updatedAt` timestamp NULL DEFAULT NULL,
  `createdAt` timestamp NULL DEFAULT NULL,
  `startDate` int NOT NULL,
  `endDate` int NOT NULL,
  PRIMARY KEY (`transaction_id`),
  KEY `identity_idx` (`identity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-14 14:36:02

通过默认设置,在构造查询时,gorm将使用snake_case约定将IdentityIdentityType字段转换为identityidentity_type列。

如果表列的命名方式不同,则需要指定以下内容。

代码语言:javascript
复制
// struct to model database table
type Transactions struct {
        Identity string `gorm:"index;not null"`
        IdentityType string `gorm:"column:identityType"`
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71469114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档