Foreign Key makes Problems

Helge Meiering posted 1 year ago

Hello, I would like to program an inventory system and need 3 tables for it. These can also be generated via artisan without a foreign key. But as soon as I want to add a foreign key, I get the following error message.

SQLSTATE[HY000]: General error: 1005 Can't create table `inventar`.`#sql-fd4_141` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `items` add constraint `items_lend_foreign` foreign key (`lend`) references `lending` (`id`))

Here my Code:

Item Table

Schema::create('items', function (Blueprint $table) {



        Schema::table('items',function ($table){


Lending Table

 Schema::create('lending', function (Blueprint $table) {



        Schema::table('lending',function ($table){



 Schema::create('persons', function (Blueprint $table) {


I've also googled, but found no solution that works for me.

Hector Tornos replied 1 year ago

I always use Laravel "best practice" notation.

Model: Item Table: items PK: id FK: lendings_id (lendings is the name of related table)

Model: Lending Table: lendings PK:id FK:person_id

Model: Person Table: persons PK: id

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us