Dodi Khtm
Published in : 2022-01-27

I want to make multiple foreign keys in Laravel, can I?

Laravel

my table

 */ public function up() { Schema::create('msd', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->bigIncrements('id'); // $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade'); $table->string('ididid')->nullable(); $table->string('hdhdjdj')->nullable(); $table->string('jdjdjdj')->nullable(); $table->string('jjdssaCBN')->nullable(); $table->timestamps(); $table->foreignId('users_id')->constrained('users'); $table->foreignId('names_id')->constrained('names'); });

I have a problem when migrating tables

C:\Users\MOHAMMED SERELHTM\Desktop\laravel\cont> php artisan migrateMigration table created successfully.Migrating: 2019_12_14_000001_create_personal_access_tokens_tableMigrated: 2019_12_14_000001_create_personal_access_tokens_table (773.49ms)Migrating: 2022_01_15_191443_create_users_tableMigrated: 2022_01_15_191443_create_users_table (278.24ms)Migrating: 2022_01_22_151524_create_names_tableMigrated: 2022_01_22_151524_create_names_table (318.44ms)Migrating: 2022_01_25_082416_msd Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1005 Can't create table `me`.`msd` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `msd` add constraint `msd_names_id_foreign` foreign key (`names_id`) references `names` (`id`)) at C:\Users\MOHAMMED SERELHTM\Desktop\laravel\cont\vendor\laravel\framework\src\Illuminate\Database\Connection.php:703 699▕ // If an exception occurs when attempting to run a query, we'll format the error 700▕ // message to include the bindings with SQL, which will make this exception a 701▕ // lot more helpful to the developer instead of just the database's errors. 702▕ catch (Exception $e) { ➜ 703▕ throw new QueryException( 704▕ $query, $this->prepareBindings($bindings), $e 705▕ ); 706▕ } 707▕ } 1 C:\Users\MOHAMMED SERELHTM\Desktop\laravel\cont\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `me`.`msd` (errno: 150 "Foreign key constraint is incorrectly formed")") 2 C:\Users\MOHAMMED SERELHTM\Desktop\laravel\cont\vendor\laravel\framework\src\Illuminate\Database\Connection.php:492 PDOStatement::execute()

And this table

in my laravel 8

How do I make two foreign keys in one table?

Comments

Mohamed Atef Date : 2022-01-27

Best answers

51

Best answers

51

errno: 150 "Foreign key constraint is incorrectly formed")

this error means that you have a problem in the format of the key constraint so can you please set a correct format to the like 

$table->unsignedBigInteger(userID');

You can try with code like this but please change the name of the table & the columns & the keys

 public function up(){ Schema::create('forums', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->id(); // $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade'); $table->string('ididid')->nullable(); $table->string('hdhdjdj')->nullable(); $table->string('jdjdjdj')->nullable(); $table->string('jjdssaCBN')->nullable(); $table->timestamps(); // Change the name of the table & columns $table->unsignedBigInteger('userID'); $table->foreign('userID')->references('id')->on('users')->onDelete('cascade'); $table->timestamps(); }); }

Dodi Khtm Date : 2022-01-27

I did as you said but it shows me an error
I want two external keys, not one but two

Mohamed Atef Date : 2022-01-27

If you got the same format so the issue is in the other table or the format of the ID in the other table, let's try to change it make it like regular ID

Dodi Khtm Date : 2022-01-27

Thank you, you solved my problem, you are great, you are the best person

Mohamed Atef Date : 2022-01-27

You are welcome :)

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Increase Sanctum token length in Laravel x9
Publish date: 2022-07-16 | Comments: 2
Get user location (city - country) from his IP address
Publish date: 2022-03-02 | Comments: 2
Laravel redirect to the last location after login
Publish date: 2022-02-21 | Comments: 1
Laravel API POST method returns 419
Publish date: 2022-08-03 | Comments: 1