laravel 5.2.7中的时间戳具有默认的空值

问题描述 投票:2回答:4

我正在使用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作为enter image description here。我去了La​​ravel 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

php mysql laravel-5.2
4个回答
3
投票

如果在模式构建器中使用created_at而不是updated_at,则默认值为NULL,它不会是Default

同样地存储记录的更新时间:enter image description here

不同之处仅在于一个字母's'并且必须提到列名,例如$table->timestamp('created_at') / $table-timestamps()CURRENT_TIMESTAMP / NULL


1
投票

不确定这是否回答了问题,但......

默认的$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

1
投票

就像单挑一样,因为Laravel 5.2.14时间戳默认为可以为空。

Blueprint.php:

timestamp

围绕这个的讨论可以在Github上阅读。

Taylor Otwell:我个人认为 - > timestamps()应该默认为 - > nullableTimestamps()来解决这个问题。

来自Github问题nullable

更改提交:默认情况下,时间戳可以为空,NULL


0
投票

它不允许你在$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(); }

© www.soinside.com 2019 - 2024. All rights reserved.