遷移檔案$table->increments('id');$table->foreign('user_id')->references('id')->on('users');$table->foreign('band_id')->references('id')->on('bands');$table->foreign('genre_id')->references('id')->on('genres');$table->foreign('cate_id')->references('id')->on('cates');$table->foreign('type_id')->references('id')->on('types');$table->integer('status');$table->date('date');$table->time('time');$table->decimal('price');$table->tinyIncrements('instrument');$table->string('instrument_detail',255);$table->timestamps();運行php artisan后遷移SQLSTATE [42000]:語法錯誤或訪問沖突:1075不正確的表定義;默認值為0。只能有一個自動列,并且必須將其定義為鍵(SQL:創建表bookings(idint unsigned not null auto_increment主鍵,statusint不為null,date 日期不為null,time時間不為null,price十進制(8,2)不為null, instrumenttinyint unsigned不為null auto_increment主鍵, instrument_detailvarchar(255)不為null,created_at時間戳為null,updated_at時間戳為null)默認字符集utf8mb4整理utf8mb4_unicode_ci)而這下面SQLSTATE [42000]:語法錯誤或訪問沖突:1075不正確的表定義;默認值為0。只能有一個自動列,并且必須將其定義為鍵
2 回答

白豬掌柜的
TA貢獻1893條經驗 獲得超10個贊
下句話:
$table->foreign('user_id')->references('id')->on('users');
只是告訴數據庫在父/外部列之間建立鏈接,但是為了做到這一點,該列必須先前存在,所以:
$table->unsignedInteger('user_id'); // first this $table->foreign('user_id')->references('id')->on('users'); // then this
您應該為每個外鍵執行此操作。
筆記:
Laravel不需要您定義此鏈接,因為它不需要使用此鏈接,僅出于數據庫一致性的考慮。
- 2 回答
- 0 關注
- 201 瀏覽
添加回答
舉報
0/150
提交
取消