Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Migration, Invalid default value when integer

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

like image 256
WindSaber Avatar asked May 19 '15 14:05

WindSaber


1 Answers

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);
like image 181
Yasen Zhelev Avatar answered Oct 20 '22 16:10

Yasen Zhelev