Search code examples
mysqllaravelmigrationdatabase-migration

getting Syntax error or access violation: 1067 Invalid default value for 'deadline' in laravel migrate


I'm migrating table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateExamsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('exams', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->enum('type',['iq','math','geo','gen']);
            $table->string('notes');
            $table->boolean('vip');
            $table->string('rate');
            $table->integer('hardness');
            $table->string('quode');
            $table->timestamp('bornline');
            $table->timestamp('deadline', $precision = '0000-00-00');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('exams');
    }
}

the cmd returning this error

Illuminate\Database\QueryException

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'deadline' (SQL: create table exams (id bigint unsigned not null auto_increment primary key, name varchar(191) not null, type enum('iq', 'math', 'geo', 'gen') not null, notes varchar(191) not null, vip tinyint(1) not null, rate varchar(191) not null, hardness int not null, quode varchar(191) not null, bornline timestamp not null, deadline timestamp not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

at C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:705
701▕ // If an exception occurs when attempting to run a query, we'll format the error
702▕ // message to include the bindings with SQL, which will make this exception a
703▕ // lot more helpful to the developer instead of just the database's errors.
704▕ catch (Exception $e) { ➜ 705▕ throw new QueryException(
706▕ $query, $this->prepareBindings($bindings), $e
707▕ );
708▕ }
709▕ }

C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:494 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'deadline'")

C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:494 PDOStatement::execute()


Solution

  • Fractional seconds precision (fsp) doesn't have a format like 0000-00-00. The fsp value, if given, must be in the range 0 to 6.

    With 0000-00-00 format, I believe that you want to create a default value for deadline column. You can use default() method :

    $table->timestamp('deadline')->default('0000-00-00');
    

    Or nullable() method, to allow NULL values to be entered into a column :

    $table->timestamp('deadline')->nullable();