我遇到的问题是,结果集中返回的tx没有所有字段集。不知道为什么吗?它在实际数据库表中有值。
// 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:事务表模式
-- 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;发布于 2022-03-14 14:36:02
通过默认设置,在构造查询时,gorm将使用snake_case约定将Identity和IdentityType字段转换为identity和identity_type列。
如果表列的命名方式不同,则需要指定以下内容。
// struct to model database table
type Transactions struct {
Identity string `gorm:"index;not null"`
IdentityType string `gorm:"column:identityType"`
}https://stackoverflow.com/questions/71469114
复制相似问题