我正在使用qazxsw poi。我创建了一个数据库迁移,如下所示:
Laravel Framework version 5.2.7
我进入虚拟机的MySql数据库并使用命令<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLessonsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('lessons', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->boolean('some_bool');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('lessons');
}
}
,这是我得到的:desc lessons;
我不明白为什么qazxsw poi和qazxsw poi专栏有qazxsw poi作为。我去了Laravel 5.2文档并看到了这个:
created_at
updated_at
所以根据文件NULL
不应该允许Default
作为$table->timestamps(); Adds created_at and updated_at columns.
。但我仍然得到$table->nullableTimestamps(); Same as timestamps(), except allows NULLs.
和$table->timestamps();
colums有NULL
作为Default
。我糊涂了。请帮忙!!
事实上,这些列应该像这个表:qazxsw poi
不确定这是否回答了问题,但......
默认的$table->timestamp('updated_at')
方法(至少在Laravel 5.4中)输出以下SQL:
created_at
这是你在寻找什么。如果将created
列设置为updated_at
,则默认值为updated
以及允许为null。
所以:
timestamp
会给出以下SQL:
`my_new_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
就像单挑一样,因为Laravel 5.2.14时间戳默认为可以为空。
Blueprint.php:
timestamp
围绕这个的讨论可以在Github上阅读。
Taylor Otwell:我个人认为 - > timestamps()应该默认为 - > nullableTimestamps()来解决这个问题。
来自Github问题nullable
更改提交:默认情况下,时间戳可以为空,NULL
它不允许你在$table->timestamp('my_new_timestamp')->nullable();
列中看到的`my_new_timestamp` timestamp NULL DEFAULT NULL
,它们都被设置为 /**
* Add nullable creation and update timestamps to the table.
*
* @param int $precision
* @return void
*/
public function timestamps($precision = 0)
{
$this->timestamp('created_at', $precision)->nullable();
$this->timestamp('updated_at', $precision)->nullable();
}