Laravel 表更新问题一般错误:1364 字段'start_date'没有默认值
Posted
技术标签:
【中文标题】Laravel 表更新问题一般错误:1364 字段\'start_date\'没有默认值【英文标题】:Laravel table update issue General error: 1364 Field 'start_date' doesn't have a default valueLaravel 表更新问题一般错误:1364 字段'start_date'没有默认值 【发布时间】:2021-11-23 15:19:45 【问题描述】:希望您平安无事,一切顺利。我正面临 laravel 关系表的问题。我有三个表,USER、CLIENT、ORDER 和 CAR_PARKING。现在 Client 与 user 相关,ORDER 与 Client 和 USER 相关,而 CAR_PARKING 仅与 ORDER 相关。我面临的类似问题是,当我尝试更新表 ORDER 时,它说 #General error: 1364 Field 'start_date' doesn't have a default value# 下面是我不同的表模型和控制器
public function updateOrderCarParking(Request $request, $id)
if (Auth::check())
$carParkingData = $request->only('removed', 'removed_date');
$validateCarParking = Validator::make($carParkingData, [
'removed' => 'required|boolean',
'removed_date' => 'nullable'
]);
if ($validateCarParking->fails())
return response()->json($validateCarParking->errors(), 422);
$orderData = $request->only('paid', 'amount_paid', 'overdue', 'currency', 'user_id');
$validateOrder = Validator::make($orderData, [
'amount_paid' => 'required|regex:/^\d*+(\.\d1,2)?$/',
'currency' => [
'required',
Rule::in(['USD', 'CAD'])
],
"paid" => "required|boolean",
'overdue' => 'regex:/^\d*+(\.\d1,2)?$/'
]);
if ($validateOrder->fails())
return response()->json($validateOrder->errors(), 422);
$updateCarParking = CarParking::updateOrCreate([
'removed' => $request->removed,
'removed_date' => $request->removed_date,
]);
$order = Order::find($id);
$order->carParkings()->save($updateCarParking);
$updateOrder = Order::find($id);
$updateOrder->amount_paid = $request->amount_paid;
$updateOrder->paid = $request->paid;
$updateOrder->currency = $request->currency;
$updateOrder->overdue = $request->overdue;
$updateOrder->user_id = Auth::user()->id;
$updateOrder->save();
if ($order && $updateOrder)
return response()->json([
'success' => true,
'message' => 'Order updated successfully',
'data' => $order
], Response::HTTP_OK);
else
return response()->json([
'success' => false,
'message' => 'Can not update',
], Response::HTTP_UNAUTHORIZED);
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
use HasFactory;
protected $fillable = ['order_type', 'amount_paid','client_id', 'user_id', 'price', 'currency', 'paid', 'overdue'];
public function user()
return $this->belongsTo(User::class);
public function client()
return $this->belongsTo(Client::class);
public function carParkings()
return $this->hasMany(CarScrap::class);
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CarParking extends Model
use HasFactory;
protected $fillable = ['start_date', 'end_of_free_charge', 'order_id', 'removed', 'removed_date'];
public function order()
return $this->belongsTo(Order::class);
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrdersTable extends Migration
/**
* Run the migrations.
*
* @return void
*/
public function up()
Schema::create('orders', function (Blueprint $table)
$table->id();
$table->decimal('price', 8, 2);
// $table->date('start_date');
$table->enum('order_type', ['Storage rent', 'Forklift', 'Ramp', 'Car Parking', 'Car Scrap', 'Shipping']);
$table->enum('currency', ['USD', 'CAD']);
$table->boolean('paid')->default('0');
$table->decimal('amount_paid')->default(0);
$table->decimal('overdue')->nullable();
$table->foreignId('client_id')->constrained('clients')->onDelete('cascade');
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->timestamps();
);
Schema::enableForeignKeyConstraints();
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
Schema::table('orders', function (Blueprint $table)
$table->dropConstrainedForeignId("client_id");
$table->dropConstrainedForeignId("user_id");
);
Schema::dropIfExists('orders');
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCarParkingsTable extends Migration
public function up()
Schema::create('car_parkings', function (Blueprint $table)
$table->id();
$table->date('start_date');
$table->date('end_of_free_charge');
$table->boolean('removed')->default("0");
$table->date('removed_date')->nullable();
$table->timestamps();
$table->foreignId('order_id')->constrained('orders')->onDelete('cascade');
);
Schema::enableForeignKeyConstraints();
public function down()
Schema::table('car_parkings', function (Blueprint $table)
$table->dropConstrainedForeignId("order_id");
);
Schema::dropIfExists('car_parkings');
我做错了什么?
【问题讨论】:
在订单模式中,您没有为“开始日期”设置默认值,没有它就保存了订单。 【参考方案1】:我们怎么能想象仅仅两行代码就解决了这些错误。无需在任何列上设置可空值。我通过执行以下代码得到了解决方案:
$client = Client::findOrFail($client->id);
$client->update($request->all());
但我在模型中加入了关系函数。我的意思是在我放置的 CLIENT 模型中
public function orders()
return $this->hasMany(Order::class);
订单模型也是如此
【讨论】:
【参考方案2】:在数据库迁移中将 start_date 设置为 Nullable。
示例:
$table->date('start_date')->nullable();
【讨论】:
您好,这里的主要问题是,我不想更新我的 CAR_PARKING 表中的 start_date 值。它必须是我在创建它时所拥有的价值。我不知道你是否明白我的意思。我之前已经创建了它,如果我想更新该表,我不想更新 start_date 列。使其可以为空,可能会更改以前的值。 我什至已经这样做了,它说:一般错误:1364 字段'order_id'没有默认值(SQL:插入car_parkings
(removed
,removed_date
,@ 987654325@, created_at
) 值 (1, ?, 2021-10-02 08:35:00, 2021-10-02 08:35:00))。 car_parking 表中的 order_id 不能为空!!!它是外键
如果不更新 start_date 的值,则该值不会为空。
我试试你早说的,但它也警告外键(order_id)没有默认值。我也应该在那里添加可为空的????【参考方案3】:
用这个替换你现有的方法:
class CreateCarParkingsTable extends Migration
public function up()
Schema::create('car_parkings', function (Blueprint $table)
$table->id();
$table->date('start_date')->nullable();
$table->date('end_of_free_charge');
$table->boolean('removed')->default("0");
$table->date('removed_date')->nullable();
$table->timestamps();
$table->foreignId('order_id')->constrained('orders')->onDelete('cascade');
);
Schema::enableForeignKeyConstraints();
public function down()
Schema::table('car_parkings', function (Blueprint $table)
$table->dropConstrainedForeignId("order_id");
);
Schema::dropIfExists('car_parkings');
如果您在任何其他字段中出现错误,则将其设为 可为空,就像我在 start_date
中所做的那样【讨论】:
您好,这里的主要问题是,我不想更新我的 CAR_PARKING 表中的 start_date 值。我想更新 CAR_PARKING 表中的某些列,而不是所有内容 我什至已经这样做了,它说:一般错误:1364 字段 'order_id' 没有默认值(SQL:插入 car_parkings(removed、removed_date、updated_at、created_at)值(1 , ?, 2021-10-02 08:35:00, 2021-10-02 08:35:00))。 car_parking 表中的 order_id 不能为空!!!它是外键以上是关于Laravel 表更新问题一般错误:1364 字段'start_date'没有默认值的主要内容,如果未能解决你的问题,请参考以下文章
SQLSTATE [HY000]:一般错误:1364 字段 'uID' 没有默认值
SQLSTATE [HY000]:一般错误:1364 字段“author_id”没有默认值