Laravel5.4 migrate时报错: Syntax error or access violation: 1067 Invalid default value for ‘published_at’ (SQL: create table `posts`

php artisan migrate
Migration table created successfully.

In Connection.php line 647:

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for ‘published_at’ (SQL: create table `posts` (`id` in
t unsigned not null auto_increment primary key, `slug` varchar(255) not null, `title` varchar(255) not null, `content` text not null
, `created_at` timestamp null, `updated_at` timestamp null, `published_at` timestamp not null) default character set utf8mb4 collate
utf8mb4_unicode_ci)
In Connection.php line 449:

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for ‘published_at’

大意是创建Posts表的时候published_at设置了错误的默认值, published_at timestamp not null引发的.

我们修改 xxxx_xx_xx_xxx_create_posts_table.php

Schema::create(‘posts’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(“slug”)->unique();
$table->string(“title”);
$table->text(“content”);
$table->timestamps();
$table->timestamp(“published_at”)->index();
});

最后一句 $table->timestamp(“published_at”)->index();设置默认值

$table->timestamp(“published_at”)->default(DB::raw(‘CURRENT_TIMESTAMP’))->index();

然后删除 数据库中已经创建的表, 再执行 php artisan migrate

发表评论

电子邮件地址不会被公开。 必填项已用*标注