Src Src - 1 month ago 14
MySQL Question

Laravel Migration

I created a migration:

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);
});
}


And when i'm trying to executed command

php artisan migrate


I'm getting error:

[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'


What i noticed is that every integer type gets
auto_increment primary key
in sql query. I think it could be the problem, as far as know that auto increment columns can't have default values.

Answer

If you take a look at the source code for the unsignedInteger method, you'll notice the second parameter doesn't specify length, but rather is for an autoIncrement boolean. You'll need to update your various integer methods accordingly.