Laravel Migration

时间:2016-10-20 20:25:13

标签: php mysql sql laravel

我创建了一个迁移:

public function up()
{
    Schema::create('accounts', function(Blueprint $table) {
        $table->increments('id')->primary()->nullable(false)->index();
        $table->string('username', 32)->nullable(false)->index();
        $table->string('sha_pass_hash', 40)->nullable(false);
        $table->string('sessionkey', 80)->nullable(false);
        $table->string('v', 64)->nullable(false);
        $table->string('s', 64)->nullable(false);
        $table->string('token_key', 100)->nullable(false);
        $table->string('email', 255)->nullable(false)->unique();
        $table->timestamp('joindate')->nullable(false)->useCurrent();
        $table->string('last_ip', 15)->nullable(false)->default('127.0.0.1');
        $table->string('last_attempt_ip', 15)->nullable(false)->default('127.0.0.1');
        $table->unsignedInteger('failed_logins', 10)->nullable(false)->default(0);
        $table->unsignedTinyInteger('locked', 3)->nullable(false)->default(0);
        $table->timestamp('last_login')->nullable(false)->default('0000-00-00 00:00:00');
        $table->tinyInteger('online', 3)->nullable(false)->default(0);
        $table->unsignedTinyInteger('expansion', 3)->nullable(false)->default(6);
        $table->bigInteger('mutetime', 20)->nullable(false);
        $table->string('mutereason', 255)->nullable(false);
        $table->string('muteby', 50)->nullable(false);
        $table->unsignedTinyInteger('locale', 50)->nullable(false);
        $table->string('os', 3)->nullable(false);
        $table->unsignedInteger('recruiter', 10)->nullable(false);
        $table->unsignedInteger('battlenet_account', 10)->nullable(true)->default(NULL);
        $table->tinyInteger('battlenet_index', 3)->nullable(true)->default(NULL);
    });
}

当我试图执行命令时

php artisan migrate

我收到错误:

[Illuminate\Database\QueryException]                                                                                                                 
  SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'failed_logins' (SQL: create table `accounts` (`id` int unsigned   
  not null auto_increment primary key, `username` varchar(32) not null, `sha_pass_hash` varchar(40) not null, `sessionkey` varchar(80) not null, `v`   
  varchar(64) not null, `s` varchar(64) not null, `token_key` varchar(100) not null, `email` varchar(255) not null, `joindate` timestamp default CURR  
  ENT_TIMESTAMP not null, `last_ip` varchar(15) not null default '127.0.0.1', `last_attempt_ip` varchar(15) not null default '127.0.0.1', `failed_log  
  ins` int unsigned not null default '0' auto_increment primary key, `locked` tinyint unsigned not null default '0' auto_increment primary key, `last  
  _login` timestamp not null default '0000-00-00 00:00:00', `online` tinyint not null default '0' auto_increment primary key, `expansion` tinyint uns  
  igned not null default '6' auto_increment primary key, `mutetime` bigint not null auto_increment primary key, `mutereason` varchar(255) not null, `  
  muteby` varchar(50) not null, `locale` tinyint unsigned not null auto_increment primary key, `os` varchar(3) not null, `recruiter` int unsigned not  
   null auto_increment primary key, `battlenet_account` int unsigned null auto_increment primary key, `battlenet_index` tinyint null auto_increment p  
  rimary key) default character set utf8 collate utf8_unicode_ci)                                                                     


 [PDOException]                                                                                     
  SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'failed_logins' 

我注意到每个整数类型在sql查询中获得auto_increment primary key。我认为这可能是问题,因为我们知道自动增量列不能有默认值。

1 个答案:

答案 0 :(得分:3)

如果您查看unsignedInteger方法的source code,您会注意到第二个参数没有指定长度,而是指定autoIncrement布尔值。您需要相应地更新各种整数方法。