i am trying to create foreign key constraint in laravel 5.8

Bhavani Itika

I can't create a foreign table key:

$table->increments('rt_id');
$table->integer('issued_id')->unsigned();
$table->foreign('issued_id')->references('issue_id')->on('book_issues');            
$table->integer('book_id')->unsigned();
$table->foreign('book_id')->references('id')->on('book_details');            

I also tried this:

$table->bigIncrements('rt_id'); 
$table->unsignedBigInteger('issued_id');  
$table->foreign('issued_id')->references('issue_id')->on('book_issues');                
$table->unsignedBigInteger('book_id');
$table->foreign('book_id')->references('id')->on('book_details');
Bhavani Itika

I just rechecked all the fields and the datatype, i observed that i used increments for primary key and in foreign key i had taken integer('book_id')->unsigned() And while migrating, i already had the primary table migrated so, i used php artisan migrate:fresh command then this time, it is migrating the foreign table time so, i'm getting the error. before my foreign key table is been placed first and then the primary key table is placed at last. So, i changed my model name and replaced according to key constraints as primary table should be stored first in migration folder even sometimes while migrating it doesn't migrates in sequences in ur folder. So, i used php artisan migrate:fresh This command is migrating all tables in sequences.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am trying to create a table with only two foreign key

Laravel 8 : Unable to create foreign key - "General error: 1215 Cannot add foreign key constraint"

Keep getting "IntegrityError FOREIGN KEY constraint failed" when i am trying to delete a post

A foreign key constraint fails on laravel 5 framework

Laravel 5 migration - foreign key constraint fails

Laravel 8 General error: 1215 Cannot add foreign key constraint

laravel 8 (errno: 150 "Foreign key constraint is incorrectly formed")

(Laravel 8) errno: 150 "Foreign key constraint is incorrectly formed

Cannot add foreign key constraint using Laravel 5 migration

MySQL 1215 Cannot add foreign key constraint - Laravel 5

General Error 1215: Cannot Add Foreign Key Constraint for Laravel 5

I'm trying to create Two Tables in sql.js but there is an error regarding foreign key constraint. there is no problem in creating the first table

Unable to create foreign key constraint?

Foreign key constraint error on create

SQL: I am trying to add a foreign key but it give me error

Why am I getting a 1064 error when I am using foreign key constraint?

Foreign Key Laravel 8 foreignId + constrained - Cannot add or update a child row: a foreign key constraint fails

I had an error when i am creating a foreign key in laravel

errno: 150 "Foreign key constraint is incorrectly formed" when trying to create a foreing key

SQL syntax error 1064 when trying to create a foreign key constraint in a create table query

Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

Laravel 5: SQLSTATE[23000]: Integrity constraint violation, foreign key constraint fails

Getting an error when trying to add foreign key constraint using Laravel migrations

Why am I getting "Cannot add foreign key constraint" error with the following sql command?

How to create a table with foreign key constraint in Liquibase?

Create table field with foreign key constraint

Unable to create foreign key constraint MySql

How to create check constraint with foreign key reference

Cannot add foreign key constraint in Laravel Migration