2017-05-16 46 views
2

我正在製作一個應用程序,但是當我插入一些relatisonship數據時,生成的查詢是反轉的,我現在不知道爲什麼。Laravel反轉插入數據透視表查詢

$last_fab->medidas()->attach(
    $last_medida->id_medida, ["medida" => $newPedidos[$i]["medidas_fab"][$j]["meters"]] 
); 

$ last_fab是Fabricacion的insance,並Fabricacion有這種關係絲毫MEDIDAS:

public function medidas() { 
    return $this->belongsToMany("App\Models\Medida", "fabricacion_medidas", "id_medida", "id_fabricacion")->withPivot("medida"); 
} 

$ last_medida是Medida的一個實例,Medida有這個關係蒙山Fabricacion:

public function fabricaciones() { 
    return $this->belongsToMany("App\Models\Fabricacion", "fabricacion_medidas", "id_fabricacion", "id_medida")->withPivot("medida"); 
} 

有移民代碼:

//fabricacion 
Schema::create("fabricacion", function (Blueprint $table) { 
    $table->engine = "InnoDB"; 

    $table->increments("id_fabricacion"); 
    $table->integer("id_order"); 
    $table->integer("id_megacart")->nullable(); 
    $table->string("reference"); 
    $table->string("name"); 
    $table->float("width"); 
    $table->float("height"); 
    $table->float("length"); 
    $table->date("date_update"); 
    $table->integer("id_categoria")->unsigned()->nullable(); 
    $table->integer("id_ubicacion")->unsigned()->nullable(); 
    $table->integer("id_incidencia")->unsigned()->nullable(); 
    $table->integer("estado")->default(1); 
    $table->timestamps(); 
}); 

//medidas 
Schema::create("medidas", function (Blueprint $table) { 
    $table->engine = "InnoDB"; 

    $table->increments("id_medida"); 
    $table->string("nom_medida")->nullable(); 
}); 

//fabricacion_medidas 
Schema::create("fabricacion_medidas", function (Blueprint $table) { 
    $table->engine = "InnoDB"; 

    $table->integer("id_fabricacion")->unsigned(); 
    $table->integer("id_medida")->unsigned(); 
    $table->integer("medida"); 
}); 

Schema::table("fabricacion_medidas", function(Blueprint $table) { 
    $table->foreign('id_fabricacion')->references('id_fabricacion')->on('fabricacion'); 
    $table->foreign('id_medida')->references('id_medida')->on('medidas'); 
}); 

這裏的錯誤它表明對我說:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: 
a foreign key constraint fails (`asensiapp`.`fabricacion_medidas`, CONSTRAINT 
`fabricacion_medidas_id_medida_foreign` FOREIGN KEY (`id_medida`) 
REFERENCES `medidas` (`id_medida`)) (SQL: insert into `fabricacion_medidas` 
(`id_fabricacion`, `id_medida`, `medida`) values (1, 10, 1.343)) 

有時是相同的錯誤,但它指的是id_fabricaccion而不是id_medida。 在值中,10是id_fabricacion,1是id_medida。所以我不知道爲什麼不在正確的位置。

+0

在ID爲10的'medidas'表中是否有記錄?因爲你的錯誤說它缺失。 –

+0

不,這是問題,我只是在帖子中添加了一行:id_fabricacion和id_medida的位置發生了變化,理論上這個順序應該沒問題。但在我的情況下被改變 – Tapasa

+0

你想更新數據透視表或創建一個新的記錄? –

回答

1

你已經在你的模型相反的關係:

return $this->belongsToMany('[ExternalClass]', '[table]', '[CurrentClassId]', '[ExternalClassId]'); 

Documentation

所以你Fabricacion變得

public function medidas() { 
    return $this->belongsToMany("App\Models\Medida", "fabricacion_medidas", "id_fabricacion", "id_medida")->withPivot("medida"); 
} 

而且你Medidas變得

public function fabricaciones() { 
    return $this->belongsToMany("App\Models\Fabricacion", "fabricacion_medidas", "id_medida", "id_fabricacion")->withPivot("medida"); 
} 
+0

哇,沒錯。我認爲在關係中外鍵是模型關係鍵,而相關鍵是本地鍵。非常感謝^^ – Tapasa