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.
JavaScript
x
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.
JavaScript
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
JavaScript
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.
JavaScript
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
JavaScript
Schema::create('items', function (Blueprint $table) {
$table->bigIncrements('id');
JavaScript
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');
});