Dodi Khtm

27 Jan 2022

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 migrate
Migration table created successfully.
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated: 2019_12_14_000001_create_personal_access_tokens_table (773.49ms)
Migrating: 2022_01_15_191443_create_users_table
Migrated: 2022_01_15_191443_create_users_table (278.24ms)
Migrating: 2022_01_22_151524_create_names_table
Migrated: 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

27 Jan 2022

githubgithubgithub
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();
 });
 }

Replies

Dodi Khtm

27 Jan 2022

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

Mohamed Atef

27 Jan 2022

githubgithubgithub

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

27 Jan 2022

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

Mohamed Atef

27 Jan 2022

githubgithubgithub

You are welcome :)

© 2024 Copyrights reserved for web-brackets.com