Php 删除级联laravel不工作
我在使用laravel 5.4删除时遇到问题 我有3个表:用户表、帖子表、向量表 用户有很多帖子 邮车 邮局有许多车辆 …无论如何…在为vehicle表创建架构时,我使用了两个外键:Php 删除级联laravel不工作,php,laravel,Php,Laravel,我在使用laravel 5.4删除时遇到问题 我有3个表:用户表、帖子表、向量表 用户有很多帖子 邮车 邮局有许多车辆 …无论如何…在为vehicle表创建架构时,我使用了两个外键: $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); $table->foreign('post_user_id')->references('user_id')->o
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('post_user_id')->references('user_id')->on('posts');
当我想删除帖子时,所有与帖子相关的车辆都将被删除……但不工作(它给出了一个关于约束的错误)
有人能告诉我我错了什么吗?这是因为我使用了两个foregin键?检查此项
$table->engine = "InnoDB";
使用一个外键解决此问题:
Schema::create('vehicles', function (Blueprint $table) {
$table->increments('id');
$table->string('mark');
$table->string('model');
$table->integer('weight');
$table->string('vehicle_state');
$table->integer('post_id')->unsigned();
$table->integer('post_user_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});
它使用的是InnoDB…顺便说一句,我使用的是mysql。我试着:删除车辆..然后在destory函数中删除帖子。lic函数
$post=post::find($id)$post->vehicles()->where('post_id',$id)->delete()$post->delete();return redirect()->route('home')代码>顺便说一句,不需要where('post_id',$id)您已经通过find获得了post约束的错误是什么?请为表之间的关系添加架构图。你的描述不正确unclear@RuChernChong当我想删除帖子时,错误是:QLSTATE[23000]:完整性约束冲突:1451无法删除或更新父行:外键约束失败。约束vehicles\u post\u user\u id\u foreign
外键(post\u user\u id
)参考posts
(user\u id
)。这里的SagarGautam是我认为的模式,因为外键post_user_id也指向post表。