我的代码有问题。我必须使用5个表中的id删除数据,这些表由主键和外键连接。
这是我尝试过的方法,但是它将显示添加订阅表ID。但是已经添加了订阅表ID。
$subscription = DB::table('tbl_asset_subscription')->where('id',$post['asset_id'])->get();
foreach($subscription as $row)
{
DB::table('tbl_asset_subscription')->where('id',$row->id)->delete();
}
$orderId = array();
foreach($subscription as $row)
{
$order = DB::table('tbl_asset_order')->where('subscription_id',$row->id)->first();
$orderId[] = $order->id;
}
foreach($orderId as $row)
{
DB::table('tbl_asset_payment')->where('order_id',$row->id)->delete();
DB::table('tbl_asset_order')->where('id',$row->id)->delete();
}
DB::table('tbl_asset_versions')->where('asset_id',$post['asset_id'])->delete();
DB::table('tbl_assets')->where('id',$post['asset_id'])->delete();
// DB::table('tbl_asset_subscription')->where('asset_id',$post['asset_id'])->delete();
echo(json_encode(array("result" => true)));
{
"message": "SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`piccoscript`.`tbl_asset_subscription`, CONSTRAINT `fk_tbl_asset_subscription_tbl_assets` FOREIGN KEY (`asset_id`) REFERENCES `tbl_assets` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) (SQL: delete from `tbl_assets` where `id` = 1)",
"exception": "Illuminate\\Database\\QueryException",
"file": "/var/www/html/piccoscript/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
"line": 664,
"trace": [
{
"file": "/var/www/html/piccoscript/vendor/laravel/framework/src/Illuminate/Database/Connection.php",
"line": 624,
"function": "runQueryCallback",
"class": "Illuminate\\Database\\Connection",
"type": "->"
},
答案 0 :(得分:0)
您可以暂时禁用外键约束
在Laravel中,您可以执行以下操作:
import {MyClass} from './MyClass'
describe('Test the MyClass:', () => {
test('invalid inputs for thefunction()', () => {
expect(MyClass.theFunction(0)).toThrow(/*'TypeError: tokens is not iterable'*/);
//Tried with and without the Error Message
});
});
答案 1 :(得分:0)
$ orderId不是关联数组,因此出现错误。试试这个代码。
foreach($subscription as $row)
{
$order = DB::table('tbl_asset_order')->where('subscription_id',$row->id)->first();
$orderId[] = array(
'id' => $order->id
);
}
foreach($orderId as $row)
{
DB::table('tbl_asset_payment')->where('order_id',$row->id)->delete();
DB::table('tbl_asset_order')->where('id',$row->id)->delete();
}
DB::table('tbl_asset_versions')->where('asset_id',$post['asset_id'])->delete();
DB::table('tbl_assets')->where('id',$post['asset_id'])->delete();
// DB::table('tbl_asset_subscription')->where('asset_id',$post['asset_id'])->delete();
echo(json_encode(array("result" => true)));