英文:
Gorm retrive different UUID value from that in the database - MSSQL
问题
模型
type Invoice struct {
ID uint `gorm:"primarykey" json:"-"`
UID uuid.UUID `gorm:"type:UNIQUEIDENTIFIER;uniqueIndex:idx_UniqueID" json:"id"`
}
在数据库中显示的值如下所示
但是当我从数据库中检索数据时,得到的值与数据库中的值不同
模型创建有问题吗?
处理程序
func GetInvoices(c *fiber.Ctx) error {
invoices := []responses.Invoice{}
var invoicesCount int64
page, pageSize, searchString, orderBy := handles.Pages(c)
switch orderBy {
case "customer_name":
orderBy = "CustomerName"
case "currency_id":
orderBy = "ForeignCurrenycID"
default:
orderBy = "ID"
}
if err := database.DB.Model(&models.Invoice{}).Select("ID").Where("InvNumber like ? or CustomerName like ? or Tax_Number like ? or Registration like ? or Description like ?", searchString, searchString, searchString, searchString, searchString).Count(&invoicesCount).Error; err != nil {
logs.ErrorLogger.Println(err.Error())
return c.Status(400).JSON(fiber.Map{"msg": err.Error()})
}
if err := database.DB.Scopes(handles.Paginate(c)).Order(orderBy).Find(&invoices).Where("InvNumber like ? or CustomerName like ? or Tax_Number like ? or Registration like ? or Description like ?", searchString, searchString, searchString, searchString, searchString).Error; err != nil {
logs.ErrorLogger.Println(err.Error())
return c.Status(400).JSON(fiber.Map{"msg": err.Error()})
}
for _, inv := range invoices{
fmt.Println(inv.UID)
}
totalPages := math.Ceil(float64(invoicesCount) / float64(pageSize))
return c.Status(200).JSON(fiber.Map{"page": page, "pageSize": pageSize, "totalPages": totalPages, "totalItems": invoicesCount, "data": &invoices})
}
英文:
Model
type Invoice struct {
ID uint `gorm:"primarykey" json:"-"`
UID uuid.UUID `gorm:"type:UNIQUEIDENTIFIER;uniqueIndex:idx_UniqueID" json:"id"`
}
On database value dispalayed as flowws
But When I retriving data from database I get different values from those in db
Is there something wrong on model creation?
handler
func GetInvoices(c *fiber.Ctx) error {
invoices := []responses.Invoice{}
var invoicesCount int64
page, pageSize, searchString, orderBy := handles.Pages(c)
switch orderBy {
case "customer_name":
orderBy = "CustomerName"
case "currency_id":
orderBy = "ForeignCurrenycID"
default:
orderBy = "ID"
}
if err := database.DB.Model(&models.Invoice{}).Select("ID").Where("InvNumber like ? or CustomerName like ? or Tax_Number like ? or Registration like ? or Description like ?", searchString, searchString, searchString, searchString, searchString).Count(&invoicesCount).Error; err != nil {
logs.ErrorLogger.Println(err.Error())
return c.Status(400).JSON(fiber.Map{"msg": err.Error()})
}
if err := database.DB.Scopes(handles.Paginate(c)).Order(orderBy).Find(&invoices).Where("InvNumber like ? or CustomerName like ? or Tax_Number like ? or Registration like ? or Description like ?", searchString, searchString, searchString, searchString, searchString).Error; err != nil {
logs.ErrorLogger.Println(err.Error())
return c.Status(400).JSON(fiber.Map{"msg": err.Error()})
}
for _, inv := range invoices{
fmt.Println(inv.UID)
}
totalPages := math.Ceil(float64(invoicesCount) / float64(pageSize))
return c.Status(200).JSON(fiber.Map{"page": page, "pageSize": pageSize, "totalPages": totalPages, "totalItems": invoicesCount, "data": &invoices})
}
答案1
得分: 1
我有同样的问题,我的数据库管理员找到了一个快速解决方法,将 GORM Select 方法中的 ID 转换为 NVARCHAR,你可以使用以下代码:
// ...
if err := database.DB.Model(&models.Invoice{}).Select("CONVERT(NVARCHAR(36), ID) as ID").Where("InvNumber like ? or CustomerName like ? or Tax_Number like ? or Registration like ? or Description like ?", searchString, searchString, searchString, searchString, searchString).Count(&invoicesCount).Error; err != nil {
logs.ErrorLogger.Println(err.Error())
return c.Status(400).JSON(fiber.Map{"msg": err.Error()})
}
// ...
我知道这不是最好的方法,但它可以工作。¯_(ツ)_/¯
英文:
I have same issue, my DBA found a quick-fix converting to NVARCHAR the ID in the GORM Select method, you could use:
// ...
if err := database.DB.Model(&models.Invoice{}).Select("CONVERT(NVARCHAR(36), ID) as ID").Where("InvNumber like ? or CustomerName like ? or Tax_Number like ? or Registration like ? or Description like ?", searchString, searchString, searchString, searchString, searchString).Count(&invoicesCount).Error; err != nil {
logs.ErrorLogger.Println(err.Error())
return c.Status(400).JSON(fiber.Map{"msg": err.Error()})
}
// ...
I know this is not the best way, but it works ¯\(ツ)/¯
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论