这是我的迁移架构:
public function up()
{
Schema::create('objects', function (Blueprint $table) {
$table->increments('id');
$table->timestamp('timestamp1');
$table->timestamp('timestamp2');
});
}
但是当我执行时php artisan migrate
,我收到此错误:
Illuminate\Database\QueryException : SQLSTATE[42000]:语法错误或访问冲突:1067“timestamp2”的默认值无效(SQL:创建表objects
(id
int unsigned not null auto_increment 主键,timestamp1
时间戳不为空,timestamp2
时间戳不为空)默认字符集utf8mb4 collate utf8mb4_unicode_ci)
我必须指出,当我删除 2 个中的一个时$table->timestamp(...);
它可以工作,但当两者都存在时就不行了。还有Object.php
模型可能是空的。我做错了吗?
我读过了这个帖子 https://stackoverflow.com/questions/35946136/laravel-eloquent-overwriting-a-custom-timestamp-why,但即使当我改变时不再有错误timestamp(...)
into dateTime(...)
,我只想要时间戳。
时间戳有点特殊,它们必须可以为空或者必须有默认值。所以你必须选择timestamp('timestamp1')->nullable();
or timestamp('timestamp1')->useCurrent()
或自定义默认值,例如timestamp('timestamp1')->default(DB::raw('2018-01-01 15:23'))
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)