简体   繁体   中英

Laravel showing 1215(Cannot add foreign key constraint) when using migration:fresh through ssh

Laravel 5.6.38

MySQL 5.7.23

PHP v7.2.10

In localhost PHP 7.2.4, It is working fine in localhost but in production it is showing below error.

php artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `videos` add constraint `videos_video_status_id_foreign` foreign key (`video_status_id`) references `statuses` (`status_id`))

  at /var/www/html/myapp/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
      /var/www/html/myapp/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

  2   PDOStatement::execute()
      /var/www/html/myapp/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

  Please use the argument -v to see more details.

This is how it is running in localhost

D:\work\www\myapp>php artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2018_08_29_112331_entrust_setup_tables
Migrated:  2018_08_29_112331_entrust_setup_tables
Migrating: 2018_08_31_103313_create_audits_table
Migrated:  2018_08_31_103313_create_audits_table
Migrating: 2018_09_06_125909_create_videos_table
Migrated:  2018_09_06_125909_create_videos_table
Migrating: 2018_09_12_064922_create_labels_table
Migrated:  2018_09_12_064922_create_labels_table
Migrating: 2018_09_14_073215_create_statuses_table
Migrated:  2018_09_14_073215_create_statuses_table
Migrating: 2018_09_14_114329_create_video_taggings_table
Migrated:  2018_09_14_114329_create_video_taggings_table
Migrating: 2018_09_15_105623_create_priorities_table
Migrated:  2018_09_15_105623_create_priorities_table
Migrating: 2018_09_17_044820_create_comments_table
Migrated:  2018_09_17_044820_create_comments_table
Migrating: 2018_09_24_130041_create_video_tagging_q_as_table
Migrated:  2018_09_24_130041_create_video_tagging_q_as_table

Update after discussion with @apokryfos

Now the migration files are

2014_10_12_000000_create_users_table
2014_10_12_100000_create_password_resets_table
2018_08_29_112331_entrust_setup_tables
2018_08_31_103313_create_audits_table
2018_09_06_064922_create_labels_table
2018_09_06_073215_create_statuses_table
2018_09_06_105623_create_priorities_table
2018_09_06_125909_create_videos_table
2018_09_14_114329_create_video_taggings_table
2018_09_17_044820_create_comments_table
2018_09_24_130041_create_video_tagging_q_as_table

and I am getting error

Dropped all tables successfully.
Migration table created successfully.

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `videos` add constraint `videos_video_identified_by_foreign` foreign key (`video_identified_by`) references `users` (`id`))

  at D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      D:\work\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

so, the issue is not the date on which migration files are created, it sems different.

Please use the argument -v to see more details.

I solved it some how, but I would like to confess, everyone should use Laravel migration very strictly.

What is the mistake?

  • I was working on localhost, so during development, you need a lot changes to your table structure and I also need to regenerate tables a lot of time during the process, so what I thought is, I do not need to create another migration file for each modification as, I am regenerating everything for now, When the application will be completed and I will need to make changes, in that time I will use separate migration file.

What is the problem ?

  • Laravel was creating tables as per migration file created date and it will never use your file update that :P, so when one table is created and need foreign key from another table, which is not created still now, it will produce this error.

so, if your FK is pointing on a PK, ensure the source column exists.

How you can solve it?

  • Adjust the order of migration files generation. Generate new files in proper order, copy-in the content, then delete the disordered old files.

And this is never a great idea, so please follow the rules strictly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM