0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

php artisan migrateで外部キー追加ができない

Last updated at Posted at 2020-03-05

#問題

  • commentsテーブルとpostsテーブルの2つを作ろうとしたがうまく行かない。
php artisan migrate


Migrating: 2020_03_05_085907_create_comments_table

   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `comments` add constraint `comments_post_id_foreign` foreign key (`post_id`) references `posts` (`id`))

#原因

  • migrationsテーブルの中にmigrate実行履歴が入っていた。
  • php artisan migrateをすると、migrationsテーブルの中に実行履歴が残るようだ。
  • 別の原因でphp artisan migrate:rollbackを使わずに、drop tableしたせい。

image.png

#解決

  • drop tableのときに、delete from migrationsで関連する履歴を消しておく。
  • あるいはちゃんとphp artisan migrate:rollbackをつかいましょう。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?