user image

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 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 Date : 2022-01-27

Best answers

46

Best answers

46

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

tymon/jwt-auth 0.5.12 requires illuminate/support ~5.0
Publish date: 2022-02-18 | Comments: 2
[solved] Nginx 413 Request Entity Too Large - Laravel
Publish date: 2022-02-01 | Comments: 2
Uncaught Error: Missing required parameters: sitekey Laravel
Publish date: 2022-01-08 | Comments: 2
How I can exclude private property from var_dump or object?
Publish date: 2022-02-22 | Comments: 2
[solved] Class "App\Exceptions\Client" not found
Publish date: 2022-02-09 | Comments: 2