简体   繁体   English

Laravel 5 Migration,整数时的默认值无效

[英]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: 我试图创建一个具有0作为其默认整数值的表。代码如下所示:

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 我正在检查laravel创建的SQL是什么,然后我找到了

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 正如您所看到的,laravel正在尝试使用char值( '0' )设置字段estado ,并将其作为自动增量主键

Any help will be really appreciated 任何帮助将非常感激

Remove the second parameter in the integer method. 删除integer方法中的第二个参数。 It sets the column as auto-increment. 它将列设置为自动增量。 Check the Laravel API for more detials. 检查Laravel API以获取更多详细信息。

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

$table->integer('estado')->default(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM