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
But When I retriving data from database I get different values from those in db
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})
}
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: a631807682
https://stackoverflow.com/questions/38160945/ms-sql-uniqueidentifier-with-golang-sql-driver-and-uuid https://dba.stackexchange.com/questions/121869/sql-server-uniqueidentifier-guid-internal-representation
Your uuid package should support mssql, and has nothing to do with gorm.
Comment From: wang-xuemin
建议使用:mssql.UniqueIdentifier
type TestDemo struct {
Id int `json:"id" gorm:"autoIncrement;column:Id"`
Age int `json:"age" gorm:"index;column:Age"`
Mobile string `json:"mobile" gorm:"column:Mobile"`
Email string `json:"email" gorm:"column:Email"`
CreateTime time.Time `json:"create_time" gorm:"column:createTime"`
UserId mssql.UniqueIdentifier `json:"user_id" gorm:"column:UserId;type:guid;"`
}
Comment From: hugh2632
`import ( "database/sql/driver" mssql "github.com/microsoft/go-mssqldb" )
type Guid struct { guid mssql.UniqueIdentifier }
func (j *Guid) Scan(value interface{}) error { return j.guid.Scan(value) }
func (j Guid) Value() (driver.Value, error) { return j.guid.Value() }
func (j Guid) String() string { return j.guid.String() }
func (j Guid) MarshalText() ([]byte, error) { return []byte(j.guid.String()), nil }` This works for me. Substitute uuid.UUID with this Guid struct.
Comment From: hugh2632
`import ( "database/sql/driver" mssql "github.com/microsoft/go-mssqldb" )
type Guid struct { guid mssql.UniqueIdentifier }
func (j *Guid) Scan(value interface{}) error { return j.guid.Scan(value) }
func (j Guid) Value() (driver.Value, error) { return j.guid.Value() }
func (j Guid) String() string { return j.guid.String() }
func (j Guid) MarshalText() ([]byte, error) { return []byte(j.guid.String()), nil }` This works for me. Substitute uuid.UUID with this Guid struct.