Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Use UUID with GORM.
package main
import (
_ ""
// Base contains common columns for all tables.
type Base struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"update_at"`
DeletedAt *time.Time `sql:"index" json:"deleted_at"`
// BeforeCreate will set a UUID rather than numeric ID.
func (base *Base) BeforeCreate(scope *gorm.Scope) error {
uuid, err := uuid.NewV4()
if err != nil {
return err
return scope.SetColumn("ID", uuid)
// User is the model for the user table.
type User struct {
SomeFlag bool `gorm:"column:some_flag;not null;default:true" json:"some_flag"`
Profile Profile `json:"profile"`
// Profile is the model for the profile table.
type Profile struct {
Name string `gorm:"column:name;size:128;not null;" json:"name"`
UserID uuid.UUID `gorm:"type:uuid;column:user_foreign_key;not null;" json:"-"`
func main() {
db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
db.AutoMigrate(&User{}, &Profile{})
user := &User{SomeFlag: false}
if db.Create(&user).Error != nil {
log.Panic("Unable to create user.")
profile := &Profile{Name: "New User", UserID: user.Base.ID}
if db.Create(&profile).Error != nil {
log.Panic("Unable to create profile.")
fetchedUser := &User{}
if db.Where("id = ?", profile.UserID).Preload("Profile").First(&fetchedUser).RecordNotFound() {
log.Panic("Unable to find created user.")
fmt.Printf("User: %+v\n", fetchedUser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment