,當我嘗試使用航行者麪包創作者與衆多以添加一個新的項目,以一對多的關係我得到這個錯誤一對多的關係:許多在航海管理面板
SQLSTATE [23000]:完整性約束衝突:1052列 'ID' 中 字段列表是不明確的(SQL:選擇
id
從products
內上products
加入order_product
id
=order_product
product_id
其中order_product
order_id
爲空。)(查看: d:\ WindowsFolders \文件\ PHP \航程r-test \ vendor \ tcg \ voyager \ resources \ views \ bread \ edit-add.blade.php)
我跟隨了文檔,並不確定我在哪裏解決問題。對我來說,看起來我的許多設置應該在普通的Laravel中工作,但是當我嘗試在voyager面板中添加一個新的order
項目時會引發該錯誤。而文檔實際上並沒有指定父表中額外的字段應該是什麼(我在哪裏發表評論)。
這裏是我的order
和order-pivot
表;
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->text('comment')->nullable();
$table->integer('price');
$table->boolean('sent');
$table->boolean('paid');
$table->string('products'); // This thing
$table->timestamps();
});
Schema::create('order_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('order_id')->unsigned();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products');
$table->integer('amount');
$table->timestamps();
});
}
這裏是產品表:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable();
$table->text('description');
$table->string('image');
$table->integer('price');
$table->string('slug')->unique();
$table->timestamps();
});
}
這裏是關係函數:
public function products()
{
return $this->belongsToMany(Product::class, 'order_product');
}
並且最後一個圖片從Order
BREAD部分:
換句話說,問題是代碼是什麼以及如何選擇和連接字段,而不是表本身? –
這是正確的。雖然,從技術上講,您可以從order_product表中刪除id字段,並將order_id --product_id字段的組合作爲主鍵(無論如何您都缺少來自這兩個字段的唯一約束)。 – Shadow
只是試試看,我會怎麼做呢?對於我來說,MySQL /雄辯仍然是最爲外向的編程。你的意思是複合鍵嗎? –