![](/img/trans.png)
[英]How can if fix this Laravel one to many with a custom foreign key behavior
[英]How can I use one column as multi foreign key in Laravel?
我想將一列用作兩個不同表的外鍵。 我怎樣才能在 Laravel 上做到這一點?
Schema::create('order_details', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('order_id');
$table->unsignedBigInteger('product_id');
$table->float('price');
$table->string('stock_keeping_unity');
$table->integer('quantity');
$table->foreign('order_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('order_id')
->references('id')
->on('cart_controls')
->onDelete('cascade');
$table->foreign('product_id')
->references('id')
->on('products')
->onDelete('cascade');
}
錯誤
PDOException::("SQLSTATE[HY000]: General error: 1826 Duplicate foreign key constraint name 'order_details_order_id_foreign'")
laravel 給出了相同的名稱,因為所有列的名稱都相同,只需重命名第二個外鍵
$table->foreign('order_id','order_details_order_id_foreign_cart')
->references('id')
->on('cart_controls')
->onDelete('cascade');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.