go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.94k stars 3.93k forks source link

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

Open rightjoin opened 7 months ago

rightjoin commented 7 months ago

GORM Playground Link

https://github.com/go-gorm/playground/pull/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)

javiersrf commented 7 months ago

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 commented 6 months ago

Can you please specifiy which database you're using ?