Gorm从数据库中检索到的UUID值与数据库中的值不同 – MSSQL

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

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"`
}

在数据库中显示的值如下所示

Gorm从数据库中检索到的UUID值与数据库中的值不同 – MSSQL

但是当我从数据库中检索数据时,得到的值与数据库中的值不同
Gorm从数据库中检索到的UUID值与数据库中的值不同 – MSSQL

模型创建有问题吗?

处理程序

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

Gorm从数据库中检索到的UUID值与数据库中的值不同 – MSSQL

But When I retriving data from database I get different values from those in db
Gorm从数据库中检索到的UUID值与数据库中的值不同 – MSSQL

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 ¯\(ツ)

huangapple
  • 本文由 发表于 2022年6月12日 20:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/72592090.html
匿名

发表评论

匿名网友

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

确定