字段为time.Time时插入失败,错误信息:FATA[0000] Error 1292: Incorrect datetime value: '0001-01-01 08:05:43' for column 'created' at row 1 #2106

Closed
opened 2022-01-22 08:42:40 +00:00 by chinaliuhan · 1 comment

版本
xorm.io/xorm v1.2.5

表结构

CREATE TABLE `tmp` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `ip` varchar(255) NOT NULL DEFAULT '',
  `created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

代码




type Tmp struct {
	Id      int64     `xorm:"not null pk autoincr UNSIGNED INT(11)"`
	Name    string    `xorm:"not null default '' VARCHAR(255)"`
	Ip      string    `xorm:"not null default '' VARCHAR(255)"`
	Created time.Time `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP"`
	Updated time.Time `xorm:"not null default '0000-00-00 00:00:00' TIMESTAMP"`
}

func (m *Tmp) TableName() string {
	return "tmp"
}

func AddNew() (int64, error) {

	var (
		err      error
		rowCount int64
	)
	tmp := Tmp{
		Name:    "aaa",
		Ip:      "127.0.0.1",
		Created: time.Now(),
		
	}
	rowCount, err = core.DbPool().InsertOne(&tmp)
	if rowCount > 0 && err != nil {
		return 0, err
	}

	return tmp.Id, err
}



错误信息

FATA[0000] Error 1292: Incorrect datetime value: '0001-01-01 08:05:43' for column 'created' at row 1 

版本 xorm.io/xorm v1.2.5 表结构 ```sql CREATE TABLE `tmp` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '', `ip` varchar(255) NOT NULL DEFAULT '', `created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 代码 ```go type Tmp struct { Id int64 `xorm:"not null pk autoincr UNSIGNED INT(11)"` Name string `xorm:"not null default '' VARCHAR(255)"` Ip string `xorm:"not null default '' VARCHAR(255)"` Created time.Time `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP"` Updated time.Time `xorm:"not null default '0000-00-00 00:00:00' TIMESTAMP"` } func (m *Tmp) TableName() string { return "tmp" } func AddNew() (int64, error) { var ( err error rowCount int64 ) tmp := Tmp{ Name: "aaa", Ip: "127.0.0.1", Created: time.Now(), } rowCount, err = core.DbPool().InsertOne(&tmp) if rowCount > 0 && err != nil { return 0, err } return tmp.Id, err } ``` 错误信息 ``` FATA[0000] Error 1292: Incorrect datetime value: '0001-01-01 08:05:43' for column 'created' at row 1 ```
Owner

If you want to a zero time as default just allow null.

If you want to a zero time as default just allow null.
lunny closed this issue 2023-07-23 02:52:35 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#2106
No description provided.