go-gorm / gorm

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

Can gorm return db default value after create #7035

Open ThanksSirAlex opened 2 months ago

ThanksSirAlex commented 2 months ago

Your Question

I have two fields

Created   time.Time   `gorm:"column:created;<-:false" json:"created"`
Modified   time.Time   `gorm:"column:modified;<-:false" json:"modified"`

And in db it is

 `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

Now after a record is created, I can't get the default value from db, Is there any way to get the value?

Expected answer

ivila commented 2 months ago

No, you can't, the return value from your db server is last insert id, if you want to update other value, just fetch the record from the db server and update yourself.

You may just check the design of golang official pkg database/sql as well.