8000 Create is not returning the inserted id when not using uint as its type · Issue #3153 · go-gorm/gorm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Create is not returning the inserted id when not using uint as its type #3153
Closed
@lopes-gustavo

Description

@lopes-gustavo

GORM Playground Link

go-gorm/playground#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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0