Laravel 6:违反完整性约束:1452无法添加或更新子行:外键约束失败
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel 6:违反完整性约束:1452无法添加或更新子行:外键约束失败相关的知识,希望对你有一定的参考价值。
所有人!
我需要一些帮助。我正在laravel 6中创建一个Web应用程序,允许用户通过php表单插入数据,该php表单也插入到数据库中。
[基本上,我创建一个供应商(正在运行),创建一个物料(它也在运行),然后创建采购(引用一个供应商和一个物料)。我也有一个Unit表,以指示物料是否为kg,per_unit等。
这是我的架构:
供应商模式:
Schema::create('suppliers', function (Blueprint $table) {
$table->increments('id');
$table->string('reference');
$table->string('name',255);
$table->string('address',255);
$table->text('zip_code')->nullable();
$table->string('locality');
$table->text('phone')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
材料架构:
Schema::create('materials', function (Blueprint $table) {
$table->increments('id');
$table->string('reference');
$table->string('name',255);
$table->text('description')->nullable();
$table->integer('quantity');
$table->integer('quantity_in_use')->default(0);
$table->string('image_material')->nullable();
$table->string('image_receipt')->nullable();
$table->timestamps();
});
单元架构:
Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
购买模式:
Schema::create('purchases', function (Blueprint $table) {
$table->increments('id');
$table->string('reference');
$table->datetime('date');
$table->unsignedInteger('supplier_id');
$table->unsignedInteger('material_id');
$table->integer('quantity');
$table->text('description')->nullable();
$table->double('price_per_unit');
$table->unsignedInteger('unit_id');
$table->timestamps();
});
以及我创建的关系迁移:
Schema::table('purchases', function (Blueprint $table) {
$table->foreign('supplier_id')->references('id')->on('suppliers');
$table->foreign('material_id')->references('id')->on('materials');
$table->foreign('unit_id')->references('id')->on('units');
});
问题是,每当我尝试创建购买时,都会出现此错误:
SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败(guacamaiateste
。purchases
,CONSTRAINT purchases_unit_id_foreign
外键(unit_id
))参考[C0 ](units
))(SQL:插入id
(purchases
,reference
,date
,supplier_id
,material_id
,description
,quantity
,price_per_unit
,[C0 ])值(C01,17-04-2020 00:00:00,8,5,Just a purchase,1,1,2020-04-17 04:08:00,2020-04-17 04:08:00 ))
这是我的模特:
updated_at
和我的采购主管:
created_at
我知道这很广泛,我很抱歉,但是我确实需要这方面的帮助。。。基本上这是此应用程序的重点。
尝试将class Purchase extends Model{
protected $fillable = ['reference', 'date', 'supplier_id', 'material_id', 'quantity', 'description', 'price_per_unit'];
public function supplier()
{
return $this->belongsTo('AppSupplier', 'supplier_id');
}
public function material()
{
return $this->belongsTo('AppMaterial', 'material_id');
}
public function unit()
{
return $this->belongsTo('AppUnit', 'unit_id');
}}
添加到use IlluminateHttpRequest;
use AppUnit;
use AppPurchase;
use AppSupplier;
use AppMaterial;
use AppHttpRequestsStorePurchase;
use AppHttpControllersController;
class PurchaseController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$purchases = Purchase::with(['unit', 'supplier', 'material'])->get();
return view('admin.purchases.index', [
'purchases' => $purchases,
]);
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
$units = Unit::all();
$suppliers = Supplier::all();
$materials = Material::all();
return view('admin.purchases.create', [
'units' => $units,
'suppliers' => $suppliers,
'materials' => $materials,
]);
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(StorePurchase $request)
{
$validated = $request->validated();
$validated['price'] = $validated['price_per_unit'] * $validated['quantity'];
if (empty($validated['supplier_id'])) {
$supplier = Supplier::create([
'reference' => $validated['supplier_reference'],
'name' => $validated['supplier_name'],
'address' => $validated['supplier_address'],
'locality' => $validated['supplier_locality'],
]);
$validated['supplier_id'] = $supplier->id;
}
if (empty($validated['material_id'])) {
$material = Material::create([
'reference' => $validated['material_reference'],
'name' => $validated['material_name'],
'quantity' => $validated['quantity'],
]);
$validated['material_id'] = $material->id;
}
Purchase::create($validated);
return redirect()->route('purchases.index')
->with('status', 'Compra registada com sucesso!')
->with('status-type', 'success');
}
/**
* Display the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function edit($id)
{
$units = Unit::all();
$purchase = Purchase::find($id);
$suppliers = Supplier::all();
$materials = Material::all();
return view('admin.purchases.edit', [
'units' => $units,
'purchase' => $purchase,
'suppliers' => $suppliers,
'materials' => $materials,
]);
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
public function update(StorePurchase $request, $id)
{
$validated = $request->validated();
$purchase = Purchase::find($id);
$purchase->update($validated);
return redirect()->route('purchases.index')
->with('status', 'Compra atualizada com sucesso!')
->with('status-type', 'success');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function destroy($id)
{
Purchase::destroy($id);
return redirect()->route('purchases.index')
->with('status', 'Compra apagada com sucesso!')
->with('status-type', 'success');
}
}
数组。发生此错误是由于单位表中不存在unit_id
的值。它不存在,因为$fillable
不在您的可填充数组中,因此laravel无法对其进行写入(此数组使laravel可以访问该表字段)。
以上是关于Laravel 6:违反完整性约束:1452无法添加或更新子行:外键约束失败的主要内容,如果未能解决你的问题,请参考以下文章
完整性约束违规:1452 无法添加或更新子行:外键约束失败(Laravel 应用程序)
SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败 - Laravel
:完整性约束违规:1452 无法添加或更新子行:laravel 迁移中的外键约束失败
SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败 - Laravel