I'm using Laravel Framework version 5.2.7
. I cretaed a database migration as shown below:
<?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');
}
}
I went in the virtual machine's MySql database and used the command desc lessons;
, this is what I'm getting:
I don't understand why do created_at
and updated_at
colums have NULL
as the Default
.
I went to Laravel 5.2 documentation and saw this:
$table->timestamps(); Adds created_at and updated_at columns.
$table->nullableTimestamps(); Same as timestamps(), except allows NULLs.
So according to the documentation $table->timestamps();
should not allow NULL
as Default
. But still I'm getting created_at
and updated_at
colums having NULL
as the Default
. I'm confused. Please HELP!!
Infact these colums shoul be like this table:
If you use $table->timestamp('created_at')
instead of $table-timestamps()
in your schema builder, default value will be CURRENT_TIMESTAMP
and it won't be NULL
.
Likewise to store update time of a record:
$table->timestamp('updated_at')
.
The difference is in just a letter 's' and have to mention the column name such as created_at
/created
, updated_at
/updated
.
Just as a heads-up, since Laravel 5.2.14 timestamps were made nullable by default.
Blueprint.php:
/**
* 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();
}
The discussion around this can be read on Github.
Taylor Otwell: Personally I think ->timestamps() should just default to ->nullableTimestamps() to solve this issue.
From Github issue https://github.com/laravel/framework/issues/12060#issuecomment-179249086
Changed in commit: Make timestamps nullable by default https://github.com/laravel/framework/commit/720a116897a4cc6780fa22f34d30c5986eafc581
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With