gorm: db.Create() do not touch updated_at field

Why updated_at field was updated in db.Create()? Must be used only created_at field! We just created new item!! not updated!! updated_at must be any default 0000-01-01 or nil (Updated_at *time.Time) So when the item is Created() updated_at field is nil or default, then we can use updated_at field to view not moderated rows (new data) something like SELECT * FROM table WHERE updated_at IS NULL (or WHERE updated_at is 0000-01-01 (default)), but now i need to create a new field moderated and use it. Plix improve it!!!

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 15 (5 by maintainers)

Most upvoted comments

You can customize "gorm:update_time_stamp" Callback. https://github.com/jinzhu/gorm/blob/836fb2c19d84dac7b0272958dfb9af7cf0d0ade4/callback_create.go#L31-L48

type TestTable struct {
	ID        uint `gorm:"primary_key"`
	Name      string
	CreatedAt time.Time
	UpdatedAt *time.Time
	DeletedAt *time.Time `sql:"index"`
}
db.DropTable(&TestTable{})
db.CreateTable(&TestTable{})
db.Callback().Create().Replace("gorm:update_time_stamp", func(scope *gorm.Scope) {
	if !scope.HasError() {
		now := gorm.NowFunc()
		if createdAtField, ok := scope.FieldByName("CreatedAt"); ok {
			if createdAtField.IsBlank {
				createdAtField.Set(now)
			}
		}
	}
})
if err := db.Create(&TestTable{Name: "test"}).Error; err != nil {
	return err
}
return nil
INSERT  INTO `test_tables` (`name`,`created_at`,`updated_at`,`deleted_at`) VALUES ('test','2019-07-08 16:06:42',NULL,NULL)

@franklingu I think you can use sql:"DEFAULT" tag.

func create(db *gorm.DB) error {
	db.Callback().Create().Remove("gorm:update_time_stamp")

	u := &User{Name: "test"}
	if err := db.Create(u).Error; err != nil {
		return err
	}
	fmt.Printf("INSERTED: %+v\n", u)
	return nil
}

type User struct {
	ID        int
	Name      string
	CreatedAt time.Time `sql:"DEFAULT: CURRENT_TIMESTAMP"`
	UpdatedAt time.Time `sql:"DEFAULT: CURRENT_TIMESTAMP"`
}
[2020-01-22 10:39:46]  [1.60ms]  INSERT INTO `users` (`name`) VALUES ('test')
[1 rows affected or returned ]

[2020-01-22 10:39:46]  [1.66ms]  SELECT `created_at`, `updated_at` FROM `users`  WHERE (id = 1)
[1 rows affected or returned ]

INSERTED: &{ID:1 Name:test CreatedAt:2020-01-22 01:39:46 +0000 UTC UpdatedAt:2020-01-22 01:39:46 +0000 UTC}
mysql> desc users;
+------------+--------------+------+-----+-------------------+----------------+
| Field      | Type         | Null | Key | Default           | Extra          |
+------------+--------------+------+-----+-------------------+----------------+
| id         | int(11)      | NO   | PRI | NULL              | auto_increment |
| name       | varchar(255) | NO   |     | NULL              |                |
| created_at | datetime     | NO   |     | CURRENT_TIMESTAMP |                |
| updated_at | datetime     | NO   |     | CURRENT_TIMESTAMP |                |
+------------+--------------+------+-----+-------------------+----------------+
4 rows in set (0.00 sec)

mysql> select * from users;
+----+------+---------------------+---------------------+
| id | name | created_at          | updated_at          |
+----+------+---------------------+---------------------+
|  1 | test | 2020-01-22 01:39:46 | 2020-01-22 01:39:46 |
+----+------+---------------------+---------------------+
1 row in set (0.00 sec)