升级到 laravel 5.3 后错误 datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' [英] After upgrade to laravel 5.3 error invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00'

查看:200
本文介绍了升级到 laravel 5.3 后错误 datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的项目从 5.2 升级到 laravel 5.3.现在当我想运行 php artisan migrate 我收到错误:

I upgraded my project to laravel 5.3 from 5.2. Now when I want run php artisan migrate I receive error:

SQLSTATE[22007]:日期时间格式无效:1292 日期时间不正确值:第 1 行的列created_at"的值:0000-00-00 00:00:00"(SQL:alter table messages add deleted_at timestamp null).

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1 (SQL: alter table messages add deleted_at timestamp null).

我的迁移:

  Schema::table(Models::table('messages'), function (Blueprint $table) {
        $table->softDeletes();
  });

在 Blueprint.php 中:

In Blueprint.php:

    public function softDeletes()
    {
        return $this->timestamp('deleted_at')->nullable();
    }

推荐答案

Laravel 5.3 已更新为默认使用 MySQL 严格"模式,其中包括 NO_ZERO_DATE 模式.

Laravel 5.3 was updated to use MySQL "strict" mode by default, which includes the NO_ZERO_DATE mode.

问题是您的现有数据被允许将0000-00-00 00:00:00"作为日期时间值.但是,现在您的连接正在使用不允许该值 (NO_ZERO_DATE) 的 sql 模式.当您尝试更改表以添加 deleted_at 列时,它会抱怨 created_at 列中的现有数据违规.

The issue is that your existing data was allowed to have '0000-00-00 00:00:00' as a datetime value. But, now your connection is using a sql mode that does not allow that value (NO_ZERO_DATE). When you attempt to alter the table to add the deleted_at column, it is complaining about the existing data violations in the created_at column.

理想的解决方案是修复数据库中的所有数据.也就是说,浏览您的数据库并更新您的日期时间/时间戳字段,使它们可以为空,并将它们的数据从 '0000-00-00 00:00:00' 转换为空.

The ideal solution would be to fix all the data in the database. That is, go through your database and update your datetime/timestamp fields so that they are nullable, and convert their data from '0000-00-00 00:00:00' to null.

但是,快速选项是在您的数据库连接上禁用严格"模式.打开您的 config/database.php 文件,并确保您的数据库连接显示 'strict' =>错误.

However, the quick option is to just disable "strict" mode on your database connection. Open your config/database.php file, and make sure your database connection shows 'strict' => false.

这篇关于升级到 laravel 5.3 后错误 datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆