最初は英語が苦手です。SQLSTATE [23000]:整合性制約違反:1452子行(laravel5)を追加または更新できません
以下に示すように、私はMariaDB(InnoDBは、laravel5)でテーブルを作成しました:
*ユーザテーブル
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
*記事テーブル
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->string('title');
$table->text('content');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
*タグテーブル
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug')->index();
$table->timestamps();
});
* articl e_tagテーブル
Schema::create('article_tag', function (Blueprint $table) {
$table->increments('id');
$table->integer('article_id')->unsigned();
$table->integer('tag_id')->unsigned();
$table->foreign('article_id')->references('id')->
on('articles')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->
on('tags')->onDelete('cascade');
});
ので、私はarticle_tagテーブルに値を挿入しようとすると、私が取得:
insert into `article_tag` (`article_id`, `tag_id`) values (3, 9);
*エラーメッセージ
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mmdance`.`article_tag`, CONSTRAINT `article_tag_tag_id_foreign` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE) (SQL: insert into `article_tag` (`article_id`, `tag_id`) values (1, 9))
私はこのトピックに関する他の記事を見てきました、解決策はありませんでした。
ご協力いただきありがとうございます。ありがとう。
エラーについてどう思われますか? IDを挿入していますが、参照値が参照テーブルに存在しません。 –
ああ、申し訳ありませんが分かりました。 – User8392