Gorm Find()查询未填充结果集中定义的所有字段。

huangapple go评论68阅读模式
英文:

Gorm Find() query not populating all fields defined in the defined struct holding the result set

问题

我遇到的问题是结果集中返回的tx对象没有设置所有字段。无法弄清楚为什么?实际数据库表中的值是存在的。

// 在FetchTransactionWithHighestExpiry中提供的请求对象:
type ProvisionRequest struct {
    Identity     string `json:"identity"`
    IdentityType string `json:"identityType"`
}

// 用于建模数据库表的结构体
type Transactions struct {
    Identity     string `gorm:"index;not null"`
    IdentityType string
}

// 存在问题的方法
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只设置了identity字段的值,而identityType是一个空字符串。你有什么想法,我在这里做错了什么吗?

编辑1:transactions表的模式定义

-- rp.transactions定义

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;
英文:

The problem that I've is tx returned in result set doesn't have all fields set. Not able to figure out why? It has value in the actual database table.

// 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
}

My tx has only value of identity set whereas identityType is an empty string. Any ideas what i'm doing wrong here?

Edit 1: transactions table schema

-- 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;

答案1

得分: 1

默认情况下,gorm在构建查询时会使用snake_case约定将IdentityIdentityType字段转换为identityidentity_type列。

如果您的表列名不同,您需要指定这一点。

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

更多信息请参考:gorm文档

英文:

By default, when constructing a query, gorm will use snake_case convention to convert Identity and IdentityType fields to identity and identity_type columns.

If your table columns are named differently, you need to specify this.

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

huangapple
  • 本文由 发表于 2022年3月14日 22:10:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/71469114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定