Error 1292: Incorrect datetime value: '0000-00-00' for column 'password_changed_at' at row 1

huangapple go评论68阅读模式
英文:

Error 1292: Incorrect datetime value: '0000-00-00' for column 'password_changed_at' at row 1

问题

我有一个用于 Rest API 服务器的 Gin 和 upper/db MySQL ORM。我创建了一个简单的脚本,在“第一次部署”或类似情况下创建数据库和表。但是我一直在遇到 SQL 错误。

这是我的 User 模型:

type User struct {
	ID                int64     `db:"id,omitempty" json:"id,omitempty"`
	Email             string    `db:"email" json:"email"`
	Password          string    `db:"password" json:"password,omitempty"`
	ProfileImage      string    `db:"profile_image" json:"profile_image"`
	PasswordChangedAt time.Time `db:"password_changed_at" json:"password_changed_at,omitempty"`
	CreatedAt         time.Time `db:"created_at" json:"created_at"`
}

这是我的第一个用户输入的样子:

user := models.User{
	Email:     "admin@site.com.uy",
	Password:  "password",
	CreatedAt: time.Now(),
}

我从数据库中得到的错误如下:

"sql: Scan error on column index 8, name "password_changed_at": unsupported Scan, storing driver.Value type into type *time.Time"

还有类似 "ProfileImage" 的错误。

这是我创建 Users 表的代码:

_, err = db.Exec(`CREATE TABLE IF NOT EXISTS users (
		id INT NOT NULL AUTO_INCREMENT,
		email VARCHAR(255) NOT NULL,
		password VARCHAR(255) NOT NULL,
		profile_image VARCHAR(255) DEFAULT '',
		password_changed_at TIMESTAMP DEFAULT NULL,
		created_at TIMESTAMP NOT NULL,
		PRIMARY KEY (id)
	)`)

我尝试将 password_changed_at 改为 DEFAULT CURRENT_TIMESTAMP,但是又遇到了另一个错误:

Error 1292: Incorrect datetime value: '0000-00-00' for column 'password_changed_at' at row 1

编辑:

我通过在 password_changed_atdb 标签中添加 omitempty 来修复了该错误,如下所示:

PasswordChangedAt time.Time `db:"password_changed_at" json:"password_changed_at,omitempty"`

但是现在,每当我获取一个用户时,我会遇到以下错误:

{
  "error": "sql: Scan error on column index 8, name \"password_changed_at\": unsupported Scan, storing driver.Value type <nil> into type *time.Time"
}

你能调整 SQL 代码以创建符合所需模型的 users 表吗?或者指出我看到的任何错误。

英文:

I have a Gin and upper/db MySQL ORM for the Rest API server. I have created a simple script to create the database and tables after "first deploy" or something like that. But I keep getting errors from SQL.

This is my User Model:

type User struct {
	ID                int64     `db:&quot;id,omitempty&quot; json:&quot;id,omitempty&quot;`
	Email             string    `db:&quot;email&quot; json:&quot;email&quot;`
	Password          string    `db:&quot;password&quot; json:&quot;password,omitempty&quot;`
	ProfileImage      string    `db:&quot;profile_image&quot; json:&quot;profile_image&quot;`
    PasswordChangedAt time.Time `db:&quot;password_changed_at&quot; json:&quot;password_changed_at,omitempty&quot;`
    CreatedAt         time.Time `db:&quot;created_at&quot; json:&quot;created_at&quot;`
}

And this is what my first user input looks like:

user := models.User{
		Email:     &quot;admin@site.com.uy&quot;,
		Password:  &quot;password&quot;,
		CreatedAt: time.Now(),
	}

I am getting errors from the database like:

> "sql: Scan error on column index 8, name &quot;password_changed_at&quot;: unsupported Scan, storing driver.Value type <nil> into type *time.Time"

Also got the error with "ProfileImage" etc.

This is my Users table creation:

_, err = db.Exec(`CREATE TABLE IF NOT EXISTS users (
		id INT NOT NULL AUTO_INCREMENT,
		email VARCHAR(255) NOT NULL,
		password VARCHAR(255) NOT NULL,
		profile_image VARCHAR(255) DEFAULT &#39;&#39;,
		password_changed_at TIMESTAMP DEFAULT NULL,
		created_at TIMESTAMP NOT NULL,
		PRIMARY KEY (id)
	)`)

I have tried changing the password_changed_at to DEFAULT CURRENT_TIMESTAMP but it gives me another error:

> Error 1292: Incorrect datetime value: '0000-00-00' for column 'password_changed_at' at row 1

Edit:

I fixed that error by adding omitempty to db tag for password_changed_at like:

    PasswordChangedAt time.Time `db:&quot;password_changed_at&quot; json:&quot;password_changed_at,omitempty&quot;`.

But now, whenever I get a user I get the following error:

{
  &quot;error&quot;: &quot;sql: Scan error on column index 8, name \&quot;password_changed_at\&quot;: unsupported Scan, storing driver.Value type &lt;nil&gt; into type *time.Time&quot;
}

Could you adjust the SQL code to create the users table for the desired model? Or indicate me any errors you see.

答案1

得分: 1

这个错误通常是由于尝试创建两个具有不同时间戳值的日期字段引起的。为了解决这个问题,我只需在受影响的表中添加以下查询。

SELECT @@GLOBAL.sql_mode global, @@SESSION.sql_mode session;
SET sql_mode = '';
SET GLOBAL sql_mode = '';

然后,您可以再次尝试编写您的CREATE表查询。

英文:

This error is normally caused by trying to create two date field with different timestamp values. To fix this I just add the following query in the affected table.

SELECT @@GLOBAL.sql_mode global, @@SESSION.sql_mode session;
SET sql_mode = &#39;&#39;;
SET GLOBAL sql_mode = &#39;&#39;;

Then you can try againg to write your CREATE table query

huangapple
  • 本文由 发表于 2022年2月21日 10:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/71200608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定