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

fieryorc picture fieryorc  路  3Comments

alanyuen picture alanyuen  路  3Comments

littletwolee picture littletwolee  路  3Comments

Ganitzsh picture Ganitzsh  路  3Comments

easonlin404 picture easonlin404  路  3Comments