13

I am trying to run migrations on my Laravel instance. They are just the default migrations (users and password resets) but when it tries to make the timestamps it throws this error:

 [Illuminate\Database\QueryException]
 SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at' (SQL: create table `
 users` (`id` int unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) n
 ot null, `password` varchar(60) not null, `remember_token` varchar(100) null, `created_at` timestamp default 0 not
 null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci)

as well as a PDOException:

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

How can I fix this?

Thanks.

2

10 Answers 10

21

I have been facing the same error. Given solutions does work properly still i want to help laravel developers. Simply add a following line to config/database.php

'mysql' => array(
   'strict'    => true
),
2
  • Whilst using a Laravel derived app (Snipe-IT), this solved my problem. Much cleaner than the other choices.
    – Anthony
    Commented May 22, 2016 at 13:30
  • I had to set 'strict' to false, but it worked all the same. Thanks.
    – HartleySan
    Commented Nov 16, 2018 at 2:30
12

This is due to MySQL not accepting zero as a valid default date and thus the table creation fails a constraint check on creation.

You probably have NO_ZERO_DATE enabled in your MySQL configuration. Setting this to off will allow you to create the table or alternatively remove the default 0 value or change it to CURRENT_TIMESTAMP.

You can find out more about this exact issue here: https://github.com/laravel/framework/issues/3602

4

it sounds like strict mode.

You may disable strict mode in one of two ways:

Open your my.ini file within the MySQL installation directory, and look for the text sql-mode.

Find:

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

and change to

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

or you can run the following in phpMyAdmin

SET @@global.sql_mode='';

4

This is due to MySQL not accepting zero as a valid default date so you can write

$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();

or $table->nullableTimestamps();

Instead of $table->timestamps();

2

This worked for me after being unsuccessful with strict mode:

$table->timestamp('published_on')->useCurrent();
2

I have used the following method:

$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP'));

Really worked!

0

Migrating old tables works like that:

Schema::table(
            'table',
            function (Blueprint $table) {
                $table->dateTime('created_at')->nullable()->default(NULL)->change();
                $table->dateTime('updated_at')->nullable()->default(NULL)->change();
            }
        );

from https://github.com/laravel/framework/issues/3602

0

you should disable MySQL strict mode on Laravel. MySQL has had a strict mode since 5.1, but in 5.7 it became the default. In Laravel, you can fix this in code: edit your database.php config file, and add a key of strict with a value of false.

for non-Laravel users:

if you're using a non-Laravel application,you won't have that option.Here's how to disable strict mode globally.find your MySQL configuration file my.cnf or my.ini the default MySQL configuration will live in /etc/mysql/my.cnf

open the file and find the [mysqld] section.We're going to add a new key, sql_mode On MySQL 5.7, the default values for this key out of the box are:

STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

The strict mode comes from STRICT_TRANS_TABLES. So, let's overwrite the sql_mode to:

[mysqld]
sql_mode=ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

That's it! Save the file, and restart MySQL.

0

You can use nullableTimestamps() instead of timestamps()

or else

$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));

also, check the database server version

Please have a look on these ref links:

https://github.com/laravel/framework/issues/3602

https://laracasts.com/discuss/channels/forge/syntax-error-or-access-violation-1067-invalid-default-value-for-created-at

0

MySQL 5.7.28

The MySQL docs recommend the following (note the use of GLOBAL):

SET GLOBAL sql_mode = 'ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.