I'm writing a Go (1.20) backend in Gin (v1.9.1) backend with Gorm (1.25.3), and for my data models I want to use UUID's as the ID. I've been trying to find a way to do this but always end up getting new errors when I try saving a new model into the Mysql database (Mysql Gorm driver V1.5.1) (using PlanetScale).
My model (User) struct looks like this: ` package models
type User struct {
gorm.Model
ID uuid.UUID gorm:"primaryKey;type:uuid;default:UUID()"
Name string
Email string
LeagueID string
League League gorm:"foreignKey:LeagueID"
}`
And my CreateUser function looks like this:
`func CreateUser(c *gin.Context) { newUser := models.User{Name: "John Doe", LeagueID: "id"}
result := initializers.DB.Create(&newUser)
if result.Error != nil {
panic("Failed to create user: " + result.Error.Error())
}
c.JSON(http.StatusCreated, newUser)
} `
The error I get from calling this CreateUser function is:
Scan: unable to scan type int64 into UUID
.
I've tried removing the gorm config on the user ID, like this:
` package models
type User struct {
gorm.Model
ID uuid.UUID gorm:"primaryKey"
Name string
Email string
LeagueID string
League League gorm:"foreignKey:LeagueID"
}`
and having the CreateUser function like this:
`func CreateUser(c *gin.Context) { newUser := models.User{ID: uuid.New(), Name: "John Doe", LeagueID: "id"}
result := initializers.DB.Create(&newUser)
if result.Error != nil {
panic("Failed to create user: " + result.Error.Error())
}
c.JSON(http.StatusCreated, newUser)
} `
But that gives me this error:
Failed to create user: Error 1265 (01000): target: <repoName>.-.primary: vttablet: Data truncated for column 'id' at row 1 (errno 1265) (sqlstate 01000) (CallerID: bg40zxyib9xefdyfnh2d): Sql: "insert into users(id, created_at, updated_at, deleted_at,
name, email, league_id) values (:vtg1 /* VARCHAR */, :vtg2 /* VARCHAR */, :vtg3 /* VARCHAR */, null, :vtg4 /* VARCHAR */, :vtg5 /* VARCHAR */, :vtg6 /* VARCHAR */)", BindVars: {REDACTED}
I have tried having the ID key a string, which gives the same error.
I also tried keeping the first id config in, gorm:"primaryKey;type:uuid;default:UUID()"
, but still creating the uuid on creation, with models.User{ID: uuid.New()...
. Then I get this ridiculous error:
Failed to create user: Error 1366 (HY000): target: <repoName>.-.primary: vttablet: Incorrect integer value: 'f9d6bd3e-f91f-446f-832f-4117731db6cb' for column 'id' at row 1 (errno 1366) (sqlstate HY000) (CallerID: bg40zxyib9xefdyfnh2d): Sql: "insert into users(created_at, updated_at, deleted_at,
name, email, league_id, id) values (:vtg1 /* VARCHAR */, :vtg2 /* VARCHAR */, null, :vtg3 /* VARCHAR */, :vtg4 /* VARCHAR */, :vtg5 /* VARCHAR */, :vtg6 /* VARCHAR */)", BindVars: {REDACTED}
which I feel doesn't make any sense.
Sorry for the long post, just wanted to include as much info as I could. I can't seem to find any good help with a google search. ChatGpt is not helping, and the Gorm docs barely mention uuid's. Should I just give up and use integer ids and auto increment?
Any help would be greatly appreciated. Thanks!