Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Migrations - Issues while creating timestamps

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.

like image 852
cheese5505 Avatar asked May 31 '15 09:05

cheese5505


People also ask

What is the difference between timestamp and timestamps?

The difference between the two is that timestamp can use CURRENT_TIMESTAMP as its value, whenever the database record is updated. timestamps doesn't take an argument, it's a shortcut to add the created_at and updated_at timestamp fields to your database.

What is the advantage of Laravel migrations?

Advantages and Disadvantages of Laravel MigrationIt is a new framework. Web applications can be built on it quickly. It is easy to learn. The documentation is easily available.

What is rollback migration in Laravel?

rollback all means it will reset all migration. so if you change anything on migration file then it will recreate and affect it. Read Also: Laravel Migration Add Comment to Column Example. php artisan migrate:reset.

Is migration necessary in Laravel?

Migrations are optional but recommended. @ecksdee if you don't add a migration which creates a table in the database then you don't need Model in Laravel terms at all, because an Eloquent model is just an object relational mapper to a table that is part of your database.


2 Answers

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

like image 156
Tom Avatar answered Sep 19 '22 09:09

Tom


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
),
like image 44
Mohan Singh Avatar answered Sep 19 '22 09:09

Mohan Singh