はじめに
今回はわたしが遭遇したエラーと解消法についてご紹介したいと思います。
なかなか解決法が見つからなかったのですが、案外あっさりとした内容でした。備忘録の意味も含めて書きたいと思います。
-各バージョン
-laravel 6.x
-PHP 7.4.9
-mySQL 5.7.30
どのようなエラーが出たか
Laravelでマグレーションをした時に、発生したエラーです。
以下がエラー内容
Illuminate\Database\QueryException  : 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 (SQL: create table `stock_cosmes` (`stock_id` bigint unsigned not null auto_increment primary key, `product` varchar(100) not null, `color` varchar(100) not null, `brand` varchar(100) not null, `price` int not null auto_increment primary key, `purchaseDate` date not null, `category` varchar(255) not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
  at /Users/yuzu/Desktop/jewelry/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|
  Exception trace:
  1   PDOException::("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")
      /Users/yuzu/Desktop/jewelry/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463
  2   PDOStatement::execute()
      /Users/yuzu/Desktop/jewelry/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463
  Please use the argument -v to see more details.
yuzunoMacBook-Air:jewelry yuzu$ -v
-bash: -v: command not found
そして、こちらが作成しようとしたテーブルです。
public function up()
    {
        Schema::create('stock_cosmes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('product', 100);
            $table->string('color', 100)->nullable();
            $table->string('brand', 100)->nullable();
            $table->integer('price', 6)->nullable();
            $table->date('purchaseDate')->nullable();
            $table->string('category');
            $table->timestamps();
        });
    }
題名にあるエラー文でググってみたのですが、bigIncrementsは主キーにしなくてはいけないや、nullにできないといった解説が多く、わたしのエラー原因に一致するものが見つかりませんでした。
そこで、Laravelの公式サイトを読み直してみると、解決することができました!
解決法
とても初歩的な内容で恥ずかしいのですが、指定しているintegerには、第二引数を設定することができないというのが、今回のエラー原因でした。
integerにも字数制限をする場合には、length()で指定できるようです。
Laravel 6.x データベース:マイグレーション
以下、修正したテーブルです。
public function up()
    {
        Schema::create('stock_cosmes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('product', 100);
            $table->string('color', 100)->nullable();
            $table->string('brand', 100)->nullable();
            $table->integer('price')->length(6)->nullable();
            $table->date('purchaseDate')->nullable();
            $table->string('category');
            $table->timestamps();
        });
    }
こちらで試したところ、問題なくエラーが解消されました!
おわりに
integerは第二引数を使用できない。とは書かれておらず、stringと同じ要領で使用してしまいましたが、改めて、公式サイトをきちんと読むことの大切さを思い知らされました。
同じようなエラーに悩まされている方のお力になれたら嬉しいです!