Framework: MIGRATION: $table->timestamp('when'); or $table->timestamps() fail when using sql_mode=TRADITIONL

Created on 14 Jan 2015  路  8Comments  路  Source: laravel/framework

when running either
$table->timestamps(); or
$table->timestamp('created_at');
on a migration, with sql_mode=TRADITIONAL I get a :

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at' (SQL: create table subscriber (id int unsigned not null auto_increment primary key, email varchar(255) not null, weekly_digest char(1) not null, source varchar(255) null, created_at timestamp default 0 not null, updated_at timestamp null) default character set utf8 collate utf8_unicode_ci)

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at'

screen shot 2015-01-14 at 20 59 21
screen shot 2015-01-14 at 20 59 05
screen shot 2015-01-14 at 20 58 52

All 8 comments

the magic happens on namespace

Illuminate\Database\Schema\Grammars;
class MySqlGrammar extends Grammar {

screen shot 2015-01-14 at 21 02 46

I don't understand why the not null timestamps are being defining with a default value
'timestamp default 0'

if the column is not null, shouldn't have any default... the user (programmer) should provide the values or a default value only/when required. None of the other types have default value by default, just this 'typeTimestamp'

created a patch (https://gist.github.com/mathiasgrimm/74f55db4791a71ce04dd) for it, however, many tests fail. All of them related to generated sql...

i think default 0 is not a good idea because it will introduce invalid dates like 0000-00-00 00:00:00 (which i particularly hate)
and there is no need for the column update_at be not null, in term of semantics, it makes no sense. If the row was first inserted is was never updated...

Currently framework just unusable with MySQL. Novadays work without TRADITIONAL sql mode is just bad practice. I wonder how such critical error goes to Laravel 5 release.

For now im using just raw statements for the migrations

DB::statement("...")

We're open to pull requests.


Disclaimer: this is not me accepting this idea. Only Taylor has the power to do this.

The best way i do to tackle this error of SQL is by adding this line "mysql => [ 'strict' => true ]"to my database.php

Why was this issue closed? It doesn't appear to have been fixed.

Was this page helpful?
0 / 5 - 0 ratings