我在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();
});
* article_tag table
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))
我已经看过关于这个主题的其他帖子,但没有解决方案 谢谢你的帮助。感谢。
答案 0 :(得分:1)
只需导入用于创建/更新记录的Schema外观。
\Schema::disableForeignKeyConstraints();
// Your query
\Schema::enableForeignKeyConstraints();
答案 1 :(得分:0)
删除外键,你很高兴。 或制作一些引用键以连接到基表