GORM Playground Link

https://github.com/go-gorm/playground/pull/1

Description

I'm slightly changing the Create example. Changing ID from uint to string make Create not return the inserted ID.

type User struct {
    ID    string 
        Name  string
    Age   uint8
}
user := User{Name: "Jinzhu", Age: 18}
_ = db.Create(&user) // pass pointer of data to Create

This yields to: INSERT INTO "users" ("id","name","age") VALUES ('','Jinzhu',18)

To make it work, I have to "force" a wrong method for default tag, like:

type User struct {
    ID   string `gorm:"default:whatever()"`
    Name string
    Age  uint8
}

This yields correctly to INSERT INTO "users" ("name","age") VALUES ('Jinzhu',18) RETURNING "id"

I couldn't find a way to set the DEFAULT keyword, as gorm will make it a string and try to insert that as "DEFAULT"

type User struct {
    ID   string `gorm:"default:DEFAULT"`
    Name string
    Age  uint8
}

This yields to INSERT INTO "users" ("id","name","age") VALUES ('DEFAULT','Jinzhu',18)


This is a breaking change, as with v1 Create would return the id without problems

Comment From: github-actions[bot]

This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs.

Comment From: jinzhu

Hi @lopes-gustavo

Just use the real db func that generates default value here?

Comment From: pkpfr

Facing the same problem. @jinzhu can you give a code example of what you mean by "just use the real db func"?

Comment From: jinzhu

@MurrayNick For example:

type User struct {
    ID   string `gorm:"default:uuid_generate_v3()"`
    Name string
    Age  uint8
}

Comment From: github-actions[bot]

This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs.

Comment From: lopes-gustavo

It does work. It just seems "wrong" since the example in the documentation works seamlessly. Maybe it's worth mentioning that on the docs? I'm sure a lot of people will face this problem, since this is a breaking change from v1.

Comment From: github-actions[bot]

This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs.

Comment From: jinzhu

Updated the doc, check it out http://v2.gorm.io/docs/create.html#Default-Values

Comment From: ctrlmaniac

I'm having the same issue. Create method does not create an ID. I'm trying your solution but when I migrate the tables it says: ERROR: function uuid_generate_v3() does not exist (SQLSTATE 42883)

Comment From: pkpfr

ERROR: function uuid_generate_v3() does not exist (SQLSTATE 42883)

Did you try v4()? v3 is not commonly used because it uses MD5. It also requires a namespace.

Comment From: ctrlmaniac

yes, it does not exist!