SkyCoder
9 months ago
218
6
Laravel

Foreign key constraint is incorrectly formed

Posted 9 months ago by SkyCoder

I am trying to restrict medicine table row deletion by making foreign key restriction on other table:

Medicine table: id, name Stock table: id, medicine_id, amount

On stock table i added:

$table->integer('medicine_id')->unsigned()->index();
$table->foreign('medicine_id')->references('id')->on('medicines')->onDelete('restrict');

And I got error:

SQLSTATE[HY000]: General error: 1005 Can't create table `dbname`.`#sql-1ee8_1f4` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `stocks` add constraint `stocks_medicine_id_foreign` foreign key (`medicine_id`) references `medicines` (`id`) on delete restrict)

What am I doing wrong? I am trying to restrict medicine deletion if there is relation with stock table.

Please sign in or create an account to participate in this conversation.