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;
CodePudding user response:
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"`
}