I have an existing mysql table, which uses mysql's UUID_SHORT() function to generate unique IDs. A simplified version of the table:
CREATE TABLE `users` (
`user_uuid` bigint(20) unsigned NOT NULL,
`user_name` varchar(64) NOT NULL
);
And a new user would be created via:
INSERT INTO users (user_uuid, user_name) values (UUID_SHORT(), "new user name");
I started to implement the DBs model using gorm and I'm drawing a blank on how to tell gorm and database/sql to call UUID_SHORT() when a new instance of User is created.
From model/users.go:
package model
type User struct {
UserUUID uint64 `gorm:"column:user_uuid;primary_key:yes";sql:"notnull;default:uuid_short"`
UserName string `sql:"notnull"`
}
func (user User) TableName() string {
return "users"
}
From model/users_test.go:
package model_test
import (
"testing"
".../model"
".../model/testutil"
)
func TestUserCreate(t *testing.T) {
user := model.User{
// UserUUID: **HOW DO I CALL UUID_SHORT() HERE?**,
UserName: "Go Test",
}
// testutil.DB is the successful result of gorm.Open("mysql", ...)
testutil.DB.Create(&user)
}
How can I call UUID_SHORT() for the user_uuid column when the instance is saved?