Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ID Primary Key of type string does not return value on DB.Create() #6947

Open
rightjoin opened this issue Apr 8, 2024 · 2 comments
Open
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@rightjoin
Copy link
Contributor

rightjoin commented Apr 8, 2024

GORM Playground Link

go-gorm/playground#713

Description

type Sample struct {
ID string ``gorm:"size:128;default:(uuid())" json:"id"``
Status string ``gorm:"size:256; default:''; not null" json:"status"``
}

This is a table in which string field gets autopopulated. Either by any trigger, or in this case by the uuid function invoked on default.

w := Sample{Status: "12345"}
err = DB.Create(&w).Error

Now I check w.ID -> instead of containing newly created UUID, its contains empty string .

If ID is int (autoincrement), then this works. But if ID is string, then it fails.

DB.Create() should be able to get the ID column (independent of its type being int or string)

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Apr 8, 2024
@javiersrf
Copy link

javiersrf commented Apr 16, 2024

Hi! I believe the erros is not related to missing ID on return. Have you tried this on ID?
Note: I believe that you will need to test using only postgresql and than add uuid extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"

ID     uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()" json:"id"`

@shedyfreak
Copy link

Can you please specifiy which database you're using ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

4 participants