2017-02-17 35 views
1

,當我嘗試使用航行者麪包創作者與衆多以添加一個新的項目,以一對多的關係我得到這個錯誤一對多的關係:許多在航海管理面板

SQLSTATE [23000]:完整性約束衝突:1052列 'ID' 中 字段列表是不明確的(SQL:選擇idproducts內上products加入 order_productid = order_productproduct_id 其中order_productorder_id爲空。)(查看: d:\ WindowsFolders \文件\ PHP \航程r-test \ vendor \ tcg \ voyager \ resources \ views \ bread \ edit-add.blade.php)

我跟隨了文檔,並不確定我在哪裏解決問題。對我來說,看起來我的許多設置應該在普通的Laravel中工作,但是當我嘗試在voyager面板中添加一個新的order項目時會引發該錯誤。而文檔實際上並沒有指定父表中額外的字段應該是什麼(我在哪裏發表評論)。

這裏是我的orderorder-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部分:

enter image description here

回答

1

在這種情況下,MySQL錯誤信息非常明確:productsorder_product表都有id字段。問題中的sql語句的選擇列表只包含id,因此MySQL無法決定應從哪個表中選擇id字段。

您需要使用表名前綴的字段名稱要清楚:

select products.id from ... 
+0

換句話說,問題是代碼是什麼以及如何選擇和連接字段,而不是表本身? –

+1

這是正確的。雖然,從技術上講,您可以從order_product表中刪除id字段,並將order_id --product_id字段的組合作爲主鍵(無論如何您都缺少來自這兩個字段的唯一約束)。 – Shadow

+0

只是試試看,我會怎麼做呢?對於我來說,MySQL /雄辯仍然是最爲外向的編程。你的意思是複合鍵嗎? –

0

我也有類似的問題,解決它通過改變從id的關係,關鍵product_id在旅行者的JSON選項內Admin