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

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

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 模型:

  1. type User struct {
  2. ID int64 `db:"id,omitempty" json:"id,omitempty"`
  3. Email string `db:"email" json:"email"`
  4. Password string `db:"password" json:"password,omitempty"`
  5. ProfileImage string `db:"profile_image" json:"profile_image"`
  6. PasswordChangedAt time.Time `db:"password_changed_at" json:"password_changed_at,omitempty"`
  7. CreatedAt time.Time `db:"created_at" json:"created_at"`
  8. }

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

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

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

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

还有类似 "ProfileImage" 的错误。

这是我创建 Users 表的代码:

  1. _, err = db.Exec(`CREATE TABLE IF NOT EXISTS users (
  2. id INT NOT NULL AUTO_INCREMENT,
  3. email VARCHAR(255) NOT NULL,
  4. password VARCHAR(255) NOT NULL,
  5. profile_image VARCHAR(255) DEFAULT '',
  6. password_changed_at TIMESTAMP DEFAULT NULL,
  7. created_at TIMESTAMP NOT NULL,
  8. PRIMARY KEY (id)
  9. )`)

我尝试将 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 来修复了该错误,如下所示:

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

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

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

你能调整 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:

  1. type User struct {
  2. ID int64 `db:&quot;id,omitempty&quot; json:&quot;id,omitempty&quot;`
  3. Email string `db:&quot;email&quot; json:&quot;email&quot;`
  4. Password string `db:&quot;password&quot; json:&quot;password,omitempty&quot;`
  5. ProfileImage string `db:&quot;profile_image&quot; json:&quot;profile_image&quot;`
  6. PasswordChangedAt time.Time `db:&quot;password_changed_at&quot; json:&quot;password_changed_at,omitempty&quot;`
  7. CreatedAt time.Time `db:&quot;created_at&quot; json:&quot;created_at&quot;`
  8. }

And this is what my first user input looks like:

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

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:

  1. _, err = db.Exec(`CREATE TABLE IF NOT EXISTS users (
  2. id INT NOT NULL AUTO_INCREMENT,
  3. email VARCHAR(255) NOT NULL,
  4. password VARCHAR(255) NOT NULL,
  5. profile_image VARCHAR(255) DEFAULT &#39;&#39;,
  6. password_changed_at TIMESTAMP DEFAULT NULL,
  7. created_at TIMESTAMP NOT NULL,
  8. PRIMARY KEY (id)
  9. )`)

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:

  1. 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:

  1. {
  2. &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;
  3. }

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

答案1

得分: 1

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

  1. SELECT @@GLOBAL.sql_mode global, @@SESSION.sql_mode session;
  2. SET sql_mode = '';
  3. 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.

  1. SELECT @@GLOBAL.sql_mode global, @@SESSION.sql_mode session;
  2. SET sql_mode = &#39;&#39;;
  3. 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:

确定