Laravel 5 Migration, Invalid default value when integer

18,738

Remove the second parameter in the integer method. It sets the column as auto-increment. Check the Laravel API for more detials.

http://laravel.com/api/5.0/Illuminate/Database/Schema/Blueprint.html#method_integer

$table->integer('estado')->default(0);
Share:
18,738
WindSaber
Author by

WindSaber

Updated on July 23, 2022

Comments

  • WindSaber
    WindSaber almost 2 years

    I was trying to create an table having a 0 as its default integer value The code looks like:

    Schema::create('gsd_proyecto', function($table) {
            $table->increments('id');
            $table->string('nombre', 80)->unique();
            $table->string('descripcion', 250)->nullable();
            $table->date('fechaInicio')->nullable();
            $table->date('fechaFin')->nullable();
            $table->integer('estado', 1)->default(0);
            $table->string('ultimoModifico', 35)->nullable();
            $table->timestamps();
        });
    

    But when I run the migration I'm getting the next error:

    Next exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'estado' 
    

    I was checking what is the SQL created by laravel and I found next

    create table `gsd_proyecto` (
     `id` int unsigned not null auto_increment primary key, 
     `nombre` varchar(80) not null, 
     `descripcion` varchar(250) null, 
     `fechaInicio` date null, 
     `fechaFin` date null, 
     `estado` int not null default '0' auto_increment primary key,   
     `ultimoModifico` varchar(35) null, 
     `created_at` timestamp default 0 not null, 
     `updated_at` timestamp default 0 not null
    )
    

    As you can see, laravel is trying to set the field estado with a char value ('0') and also as an autoincrement primary key

    Any help will be really appreciated

  • WindSaber
    WindSaber almost 9 years
    It worked! Thanks A lot! I was thinking in a definition like MySql does....instead I'm using tinyInteger now and it's working now. Thanks a lot!