我使用laravel 5.4
删除了一个问题我有3个表:用户,帖子,Vechicles
用户拥有多条帖子
车辆belognsTo Post
发布了很多车辆
...无论如何......在为车辆表创建架构时,我使用2个外键:
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('post_user_id')->references('user_id')->on('posts');
当我想要删除帖子时,所有与帖子相关的车辆都要删除....但是不起作用(它给出了关于约束的错误)
有人能告诉我我错了吗?是我使用2个外键吗?
答案 0 :(得分:0)
检查这个
$table->engine = "InnoDB";
答案 1 :(得分:0)
使用一个外键解决问题:
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');
});