我遇到的問題是結果集中回傳的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;
uj5u.com熱心網友回復:
默認情況下,在構造查詢時,gorm 會使用snake_case 約定將IdentityandIdentityType欄位轉換為identityandidentity_type列。
如果您的表列名稱不同,則需要指定此名稱。
// struct to model database table
type Transactions struct {
Identity string `gorm:"index;not null"`
IdentityType string `gorm:"column:identityType"`
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444029.html
標籤:走 grails-orm
上一篇:如何在golang中創建一個包含http客戶端的結構的模擬?
下一篇:如何匹配字串golang結構標簽
