Laravel 9 - SQLSTATE[HY000]: General error: 1364 Field 'order_id' doesn't have a default value
Laravel 9 - SQLSTATE[HY000]: General error: 1364 Field 'order_id' doesn't have a default value
你好,我正在尝试向数据库插入数据,这些数据需要分割到两个相关的表中。目前我正在使用一些虚拟值。
表1:
Schema::create('orders_grid', function (Blueprint $table) { $table->id(); $table->integer('id_agente')->unsigned()->index(); $table->tinyText('canale'); $table->integer('codice_cliente')->unsigned(); $table->integer('codice_destinazione')->unsigned(); $table->timestamp('data_ordine'); $table->date('data_richiesta')->nullable(); $table->tinyText('tipologia',1); $table->integer('edi_id')->nullable(); $table->string('edi_company',5)->nullable(); $table->string('edi_doctype',2)->nullable(); $table->integer('jde_id')->nullable(); $table->string('jde_company',5)->nullable(); $table->string('jde_doctype',2)->nullable(); $table->integer('stato_ordine')->default(0); $table->decimal('total_order',8,2)->unsigned()->nullable(); $table->text('note_consegna')->nullable(); $table->timestamps(); });
表2:
Schema::create('orders_detail', function (Blueprint $table) { $table->id(); $table->foreignId('order_id')->constrained('orders_grid')->onUpdate('cascade')->onDelete('cascade'); $table->integer('nr_riga')->unsigned(); $table->string('codice_articolo',25); $table->integer('quantita')->unsigned(); $table->decimal('prezzo',6,4)->unsigned(); $table->timestamps(); });
模型1:
protected $fillable = [ 'id_agente', 'canale', 'codice_cliente', 'codice_destinazione', 'data_ordine', 'data_richiesta', 'tipologia', 'edi_id', 'edi_company', 'edi_doctype', 'jde_id', 'jde_company', 'jde_doctype', 'stato_ordine', 'total_order', 'note_consegna', ]; public function order_detail() { return $this->hasMany('App\Models\OrderDetail'); }
模型2:
protected $fillable = [ 'nr_riga', 'codice_articolo', 'quantita', 'prezzo', 'order_id' ]; public function order_grid() { return $this->belongsTo('App\Models\OrderGrid'); }
控制器:
function importData(Request $request) { $request->validate([ 'uploaded_file' => 'required|file|mimes:xls,xlsx' ]); $excel_file = $request->file('uploaded_file'); $data_grid = []; $data_detail = []; $file = IOFactory::load($excel_file->getRealPath()); // 从Excel中获取网格数据 $data_grid['codice_cliente'] = $file->getActiveSheet()->getCell('B3')->getValue(); $data_grid['codice_destinazione'] = $file->getActiveSheet()->getCell('G3')->getValue(); $data_grid['data_ordine'] = '2022-07-15 15:32:04'; //$data_grid['data_ordine'] = $file->getActiveSheet()->getCell('B4')->getValue(); $data_grid['data_richiesta'] = '2022-06-15';//$file->getActiveSheet()->getCell('B5')->getValue(); $data_grid['note_consegna'] = $file->getActiveSheet()->getCell('G5')->getValue(); // 网格数据(静态) $data_grid['id_agente'] = 1; $data_grid['canale'] = 'B'; $data_grid['tipologia'] = 'O'; $data_grid['edi_id'] = 1; $data_grid['edi_company'] = 'C'; $data_grid['edi_doctype'] = 'D'; $data_grid['jde_id'] = 2; $data_grid['jde_company'] = 'E'; $data_grid['jde_doctype'] = 'F'; $data_grid['stato_ordine'] = 1; $data_grid['total_order'] = 99.99; // 详细数据 $data_detail['codice_articolo'] = $file->getActiveSheet()->getCell('A9')->getValue(); $data_detail['quantita'] = $file->getActiveSheet()->getCell('B9')->getValue(); // 详细数据(静态) // $data_detail['order_id'] = ; $data_detail['nr_riga'] = 50; $data_detail['prezzo'] = 99.9999; // $data_excel['Riferimento_ordine_cliente'] = $file->getActiveSheet()->getCell('G4')->getValue(); // $data_excel['Codice_prezzo_gruppo_1'] = $file->getActiveSheet()->getCell('D9')->getValue(); // $data_excel['Codice_prezzo_gruppo_2'] = $file->getActiveSheet()->getCell('E9')->getValue(); // $data_excel['Codice_prezzo_gruppo_3'] = $file->getActiveSheet()->getCell('F9')->getValue(); //dd($data_excel); $validator = Validator::make([$data_grid, $data_detail], [ 'codice_cliente' => 'max:15', 'codice_destinazione' => 'max:15', 'data_ordine' => 'max:15', 'data_richiesta' => 'max:15', 'note_consegna' => 'max:15', 'id_agente' => 'max:15', 'canale' => 'max:15', 'tipologia' => 'max:15', 'edi_id' => 'max:15', 'edi_company' => 'max:15', 'edi_doctype' => 'max:15', 'jde_id' => 'max:15', 'jde_company' => 'max:15', 'jde_doctype' => 'max:15', 'stato_ordine' => 'max:15', 'total_order' => 'max:15', 'codice_articolo' => 'max:15', 'quantita' => 'max:15', // //'order_id' => 'max:15', 'nr_riga' => 'max:15', 'prezzo' => 'max:15', ], [ 'max' => '":Attribute"字段允许的最大字符数为:max', 'required' => ':attribute是必填项!', ]); //dd($validator); if ($validator->fails()) { return redirect() ->back() ->withErrors($validator); } else { DB::table('orders_grid')->insert($data_grid); DB::table('orders_detail')->insert($data_detail); return redirect()->back()->with('message', '文件已成功上传!'); } }
为什么它希望有一个默认值,而不是从与之相关的orders_grid表中获取id?如果我添加一些虚拟值,比如在`$data_detail['order_id'] = ;`中添加1,就不会出错。但是我需要在那里获取相关的ID。
文章标题:Laravel 9 - SQLSTATE[HY000]: General error: 1364 Field 'order_id' doesn't have a default value问题的原因和解决方法
问题的原因:
在控制器的最后一个else条件中更新了代码,但出现了错误。具体代码如下:
else { $insert_id = DB::table('orders_grid')->insertGetId($data_grid); $data_detail['order_id'] = $insert_id; DB::table('orders_detail')->insert($data_detail); return redirect()->back()->with('message', 'File caricato correttamente!'); }
在这段代码中,数据成功插入到了orders_grid表中,但是在orders_detail表中未能成功插入,而是返回了错误信息"SQLSTATE[HY000]: General error: 1364 Field 'order_id' doesn't have a default value"。问题似乎是因为在orders_detail表中order_id字段没有设置默认值。
解决方法:
通过检查orders_grid表的新条目,可以获取最后插入的id,然后可以在相关的表(即orders_detail表)中使用这个id。在这个具体的问题中,解决方法是检查变量名是否正确。原本使用了$data_details['order_id'],而实际应该使用$data_detail['order_id']。修改后的代码如下:
else { $insert_id = DB::table('orders_grid')->insertGetId($data_grid); $data_detail['order_id'] = $insert_id; DB::table('orders_detail')->insert($data_detail); return redirect()->back()->with('message', 'File caricato correttamente!'); }
此时,问题得到解决,数据成功插入到了orders_detail表中。
在问题解决后,提问者还询问了关于使用Eloquent模型的方法来获取id并将其传递给第二个表格的类似解决方案。推荐了一个相关的帖子,提供了实现此目的的方法。具体方法可以参考以下链接:
- [stackoverflow.com/questions/21084833](https://stackoverflow.com/questions/21084833)
- [stackoverflow.com/questions/27873777](https://stackoverflow.com/questions/27873777)
提问者表示已经按照推荐的方法进行了修改,问题得到了解决。最后,提问者对帮助的回答表示感谢,并表示将在Stack Overflow上将正确答案标记为解决方案。