1

I'm working with existing MSSQL Server DB, it already has entries and now we are integrating GORM to the existent Go project. Before GORM, this tool used to work with this package github.com/denisenkom/go-mssqldb so we use it's uuid integrated driver, now following the GORM examples, we decided to use the google/uuid package.

But now, the uuid.UUID handle in different way the MSSQL uuid's, why is this happening?

Using google pkg:

type User struct {

  ID   uuid.UUID `json:"id" gorm:"primaryKey;column:id;type:uuid" 
sql:"type:uuid"`
  XID  int  `json:"x_id" gorm:"column:x_id;type:int`

}

x := 1
usr := User{}
db.Model(&User{}).
    Where("x_id = ?", x).
    First(&usr)

// ID -> 3913a9d1-9c08-3f4c-b816-f55c899f5617

Using Denisen pkg:

type User struct {

  ID   mssql.UniqueIdentifier `json:"id" gorm:"primaryKey;column:id;type:uuid" 
sql:"type:uuid"`
  XID  int  `json:"x_id" gorm:"column:x_id;type:int`

}

x := 1
usr := User{}
db.Model(&User{}).
    Where("x_id = ?", x).
    First(&usr)

// ID -> D1A91339-089C-4C3F-B816-F55C899F5617

Actually, if I make the raw SELECT in my MSSQL client the result is equal to Denisen pkg, please take a look the last 2 blocks of the google's uuid are the same but lower cased, the another ones are the same letters/numbers but in different places and lower cased.

Should I use some kind of configuration to use the google uuid package?

I don't want to use the Denisen pkg because it imports the full package and I just need the uuid section, and google uuid includes the scanner interfase to use with sql.

Any help??

Tks!

  • 2
    Microsoft uses GUIDs while the rest of the industry uses UUIDs. Read [Wikipedia](https://en.wikipedia.org/wiki/Universally_unique_identifier). – Basil Bourque Jan 10 '22 at 23:31
  • 2
    Looks like a case of network byte order vs host byte order. Google RFC 4122 and look at section 4.1.2. The RFC defines the first three fields to be most significant byte (MSB) first, but MS SQL stores them internally least significant byte (LSB) first. Something (I don't know what) is transferring the value without properly ordering the bytes. Not sure what the best fix is. A workaround is a custom function to swap bytes 0/3, 1/2, 4/5, 6/7. This would need to be applied in both directions. – T N Jan 11 '22 at 02:52

0 Answers0