【Laravel】マイグレーションでエラー:SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value

Laradockのロゴ画像
  • URLをコピーしました!

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value というエラーに遭遇したので、その対処法のメモ。

 

 

 

目次

エラー内容

 

php artisan migrateをしたら、以下のようなエラーに遭遇しました。

 

$ php artisan migrate
Exception trace:

1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'fav'")
/var/www/my-portfolio/vendor/laravel/framework/src/Illuminate/Database/Connection.php:452

2 PDO::prepare("create table `products` (`id` int unsigned not null auto_increment primary key, `mail` varchar(50) 
not null, `status` varchar(3) not null default '出品中', `mail_b` varchar(50) null, `img1` varchar(50) null, `img2` varchar(50) 
null, `img3` varchar(50) null, `name` varchar(50) not null, `description` text not null, `cat_id` varchar(2) not null, `state` 
varchar(4) not null, `place` varchar(4) not null, `days` varchar(3) not null, `price` int unsigned not null auto_increment primary 
key, `fee` int unsigned not null auto_increment primary key, `fav` smallint unsigned not null default '0' auto_increment primary 
key, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'")

 

 

で、migrateした内容が以下。

 

...省略...

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('mail', 50);
        $table->string('status', 3)->default('出品中');
        $table->string('mail_b', 50)->nullable();
        $table->string('img1', 50)->nullable();
        $table->string('img2', 50)->nullable();
        $table->string('img3', 50)->nullable();
        $table->string('name', 50);
        $table->text('description', 1000);
        $table->string('cat_id', 2);
        $table->string('state', 4);
        $table->string('place', 4);
        $table->string('days', 3);
        $table->unsignedInteger('price', 7);
        $table->unsignedInteger('fee', 6);
        $table->unsignedSmallInteger('fav', 3)->default(0);
        $table->timestamps();
    });
}

...省略...

 

Invalid default value for ‘fav’ と言われているので、「favのデフォルト値が無効だよ」ってことかな?

 

 

その下のPDO::prepareも読んでみると、

 

...省略...

`price` int unsigned not null auto_increment primary key, `fee` int unsigned not null 
auto_increment primary key, `fav` smallint unsigned not null default '0' auto_increment primary key,

...省略...

 

なぜか、「price」「fee」「fav」がauto_increment primary keyになってる。

 

 

解決策

 

で、色々と調べてみたら以下の記事を発見。

 

SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

 

Integerなどに対して長さを指定したい時は、第2引数で長さの指定をせずに、->length()で指定するといいらしいです。

 

...省略...

$table->unsignedInteger('price')->length(7); 
$table->unsignedInteger('fee')->length(6); 
$table->unsignedSmallInteger('fav')->length(3)->default(0);

...省略...

 

この記事を書いた人

コメント

コメント一覧 (1件)

コメントする

目次