Jump to content

migration: error 1215 Cannot add foreign key constraint


ali_254
 Share

Recommended Posts

hi ...

I made a table called: flights , Also, I want to create another table and run the "join" operation between them. this table called: flight_comments

 

table flights:

 public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();


        });

 

table flight_comments:

  public function up()
    {
        Schema::create('flight_comments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('flight_id')->unsigned();
            $table->string('body','140');
            $table->nullabletimestamps();
            $table->foreign('flight_id')->references('id')->on('flights')->onDelete('cascade');
        });
    }

 

When I run the "migration" command, I get the following error:

 

In Connection.php line 647:

  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `flight_comments` add constraint `flight_
  comments_flight_id_foreign` foreign key (`flight_id`) references `flights` (`id`) on delete cascade)


In Connection.php line 449:

  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

 

 

please guide me

Link to comment
Share on other sites

2 minutes ago, Barand said:

That was intended as a hint.

Make them both the same and you should then be able to create your foreign key.

thanks , but i delete all table in phpmyadmin and Reinstall the code "php artisan migrate" , Problem solved, thanks a lot

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.