我在项目表中遇到了一个问题,无法将条形码号码
定义为主键。之前我一直使用普通的ID作为主键。
Schema::create('items', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('price');
}
现在,当我想把id的类型更改为
整数
条形码号码时,它不起作用。Schema::create('items', function (Blueprint $table) {
$table->integer('id', 15)->unsigned;
$table->string('name');
$table->integer('price');
}
运行
php artisan migrate
后,我遇到了以下错误: SQLSTATE[HY000]: General error: 1005 Can't create table `inventory`.`orders` (errno: 150 "Foreign
key constraint is incorrectly formed")
下面的订单表(Order table)具有对商品表(Item table)的外键引用。
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('item_id')->unsigned();
$table->integer('quantity');
$table->timestamps();
$table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
});