升级到laravel 5.3后,错误的日期时间格式:1292错误的日期时间值:'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'
问题描述
我将项目从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: 修改表
messages
添加deleted_at
时间戳为空).
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
adddeleted_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"strict"模式,其中包括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.
理想的解决方案是修复数据库中的所有数据.也就是说,浏览数据库并更新datetime/timestamp字段,使它们可以为空,然后将其数据从'0000-00-00 00:00:00'转换为null.
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' => false
.
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后,错误的日期时间格式:1292错误的日期时间值:'0000-00-00 00:00:00'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!