升级到 Laravel 5.3 后错误无效日期时间格式:1292 不正确的日期时间值:'0000-00-00 00:00:00'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47946607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:09:25  来源:igfitidea点击:

After upgrade to laravel 5.3 error invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00'

laraveldatetimestampmigration

提问by Igor Ostapiuk

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

我将我的项目从 5.2 升级到 Laravel 5.3。现在,当我想运行时,php artisan migrate我收到错误消息:

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 messagesadd deleted_attimestamp null).

SQLSTATE[22007]:无效的日期时间格式:1292 不正确的日期时间值:'0000-00-00 00:00:00' 列'created_at' 在第 1 行(SQL:alter table messagesadd deleted_attimestamp null)。

My migration:

我的迁移:

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

In Blueprint.php:

在 Blueprint.php 中:

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

回答by patricus

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

Laravel 5.3 更新为默认使用 MySQL 的“严格”模式,其中包括NO_ZERO_DATE模式。

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_atcolumn, it is complaining about the existing data violations in the created_atcolumn.

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

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.

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

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

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

回答by mukade

I've had the same problem when adding a datetime column in a table with timestamps (created and updated_at).

在带有时间戳(created 和 updated_at)的表中添加日期时间列时,我遇到了同样的问题。

I solved just adding the '->nullable($value = true)' modifier in my migration:

我解决了在迁移中添加 '->nullable($value = true)' 修饰符的问题:

$table->datetime('expires_at')->nullable($value = true);

$table->datetime('expires_at')->nullable($value = true);

I understood that the database complained about inserting all that zeros if the input dont come. Add the instruction to be nullable did the trick.

我知道如果输入不来,数据库会抱怨插入所有的零。将指令添加为可为空的就行了。

回答by Hossein Bagherzadegan Talkhoun

This error Cause by STRICT_TRANS_TABLES and NO_ZERO_DATE in sqlmode for mysql 5.7

此错误由 STRICT_TRANS_TABLES 和 NO_ZERO_DATE 在 mysql 5.7 的 sqlmode 中引起

disabled it and in this way all things work correctly .

禁用它,这样一切正常。

回答by Roger Thomas Riche

I like creating the default as NOW()anyways.

NOW()无论如何,我喜欢创建默认值。

So $table->datetime('expires_at')->default(NOW());is also a good option.

所以$table->datetime('expires_at')->default(NOW());也是一个不错的选择。

回答by saber tabatabaee yazdi

when i changed my localekey,in app.php in my laravel

当我在 laravel 的 app.php 中更改我的语言环境键时

QueryException In Connection.php line 664 : SQLSTATE 22007 : Invalid datetime format: 1292 Incorrect date value: '????-??-??' for column

QueryException In Connection.php line 664 : SQLSTATE 22007 : Invalid datetime format: 1292 Incorrect date value: '????-??-??' for column

this errors happened and when change to english this gone and disappeared

这个错误发生了,当更改为英语时,它消失了