I have problem to define the barcode number
as the primary key
at Item table. Previously I use the usual ID as the primary key.
Schema::create('items', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->integer('price'); }
Now, when I want to change the type of id to integer
barcode number, it does not work.
Schema::create('items', function (Blueprint $table) { $table->integer('id', 15)->unsigned; $table->string('name'); $table->integer('price'); }
I got the error below after running php artisan migrate
SQLSTATE[HY000]: General error: 1005 Can't create table `inventory`.`orders` (errno: 150 "Foreign key constraint is incorrectly formed")
The Order table below has foreign key reference to 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'); });
Advertisement
Answer
Try this one. hopefully it will work
Schema::create('items', function (Blueprint $table) { $table->bigIncrements('id');
Schema::create('orders', function (Blueprint $table) { $table->increments('id'); $table->integer('quantity'); $table->timestamps(); $table->unsignedBigInteger('item_id'); $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade'); });