Search code examples
eloquentlaravel-8laravel-migrationsphp-7.4

Laravel - SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value


I'm creating a migration with 2 timestamps, but for some reason I can't put 2 timestamps without a default/nullable value.

My code:

        Schema::create('lessons', function (Blueprint $table) {
            $table->id();
            $table->timestamp('checkin');
            $table->timestamp('checkout');
            $table->string('url')->nullable();
            $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
            $table->softDeletes();
        });

If I put a nullable value in checkout: $table->timestamp('checkout')->nullable; my code working fine. But my two values cannot be empty.

The error:

 Illuminate\Database\QueryException 

  SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'checkout' (SQL: create table `lessons` (`id` bigint unsigned not null auto_increment primary key, `checkin` timestamp not null, `checkout` timestamp not null, `url` varchar(191) null, `created_at` timestamp not null default CURRENT_TIMESTAMP, `updated_at` timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

If a change the order the bug happen the same to checkin

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'checkin' (SQL: create table `lessons` (`id` bigint unsigned not null auto_increment primary key, `checkout` timestamp not null, `checkin` timestamp not null, `url` varchar(191) null, `created_at` timestamp not null default CURRENT_TIMESTAMP, `updated_at` timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

I think that's can be a bug in Laravel.

  • Note 1: If I remove the 'second' timestamp not nullable that's work fine.
  • Note 2: I'm using php artisan migration:fresh to get those results.

Solution

  • I solved my question with this post: https://stackoverflow.com/a/59326426/11639058

    I've change the timestamp to dateTime.