Gorm: How to use sql NOW() as default value for the column when db.Create() using struct

Created on 30 Oct 2014  路  5Comments  路  Source: go-gorm/gorm

How to use sql NOW() as default value for the column when db.Create() using struct

Most helpful comment

This works for me:

TimeCreated time.Time gorm:"default:CURRENT_TIMESTAMP"

All 5 comments

Use DEFAULT CURRENT_TIMESTAMP for sql tag?

yea, bt the problem is that gorm dont know this and keep filling in "" to my DateTime field @jinzhu

This works for me:

TimeCreated time.Time gorm:"default:CURRENT_TIMESTAMP"

how about ON UPDATE CURRENT_TIMESTAMP?

@franklingu you can use BeforeSave hook for that https://gorm.io/docs/hooks.html

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sredxny picture sredxny  路  3Comments

satb picture satb  路  3Comments

easonlin404 picture easonlin404  路  3Comments

izouxv picture izouxv  路  3Comments

rfyiamcool picture rfyiamcool  路  3Comments