laravel 7 でタグ機能を実装しようとしたのですが、テーブル作成後マイグレーションファイルをいじったら下記のエラーが出てしまい詰まってます。
SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'tags' (SQL: alter table title_tag
add constraint title_tag_tag_id_foreign
foreign key (tag_id
) references tags
(id
) on delete cascade)
実際にデータベースクライアントで確認したところ、tagsテーブルは確認できませんでした。
ですが、create_tags_table,tag.php 等は作られています。
create_tags_table
public
1 { 2 Schema::create('tags', function (Blueprint $table) { 3 $table->id(); 4 $table->timestamps(); 5 $table->string('name'); 6 }); 7 }
create_title_tag_table
public
1 { 2 Schema::create('title_tag', function (Blueprint $table) { 3 $table->id(); 4 $table->timestamps(); 5 $table->unsignedInteger('tag_id'); 6 $table->unsignedInteger('title_id'); 7 $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); 8 $table->foreign('title_id')->references('id')->on('titles')->onDelete('cascade'); 9 }); 10 }
<<追記
ご指摘のとおり、マイグレーションファイルの順番が正しい順になっていなかったので、一度マイグレーションファィルを手動で削除した後、再度作り直すと
SQLSTATE[HY000]: General error: 3780 Referencing column 'tag_id' and referenced column 'id' in foreign key constraint 'title_tag_tag_id_foreign' are incompatible. (SQL: alter table `title_tag` add constraint `title_tag_tag_id_foreign` foreign key (`tag_id`) references `tags` (`id`) on delete cascade)
このようなエラーに変わりました。
・試したこと
それぞれのIDのところをデフォルトの$table->id();に変えて migration refresh → php artisan migrateしてみましたがだめでした。
紐付けたいIDの型が違うとこのようなエラーが出るらしいのですが、それぞれの型はbigint unsigned 連番 で揃っていました。
回答1件
あなたの回答
tips
プレビュー