Eloquent Delete-SQLSTATE[22007]:无效的日期时间格式:1292截断了不正确的DOUBLE值



我收到这个错误:

SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: '808498e7-a393-42f1-ab23-6ee89eb7040a' 

当试图通过关系删除记录时,使用:

$delivery->stockMovements()->delete();

原始查询显示为:

delete from `stock_movements` where `stock_movements`.`entity_id` = 10000005 and `stock_movements`.`entity_id` is not null and `stock_movements`.`company_id` = 8b11050c-612c-4922-8b34-d04d579e02a9

我搜索了又搜索,但找不到任何特定的东西,除了可能与强制转换错误有关。也许与UUID有关?

迁移如下:

Schema::create('deliveries', function (Blueprint $table) {
$table->increments('id');
$table->uuid('company_id');
$table->string('delivery_type');
$table->string('supplier_name');
$table->string('supplier_ref')->nullable();
$table->string('merchant_ref')->nullable();
$table->string('carrier_name')->nullable();
$table->string('status');
$table->date('expected_delivery');
$table->dateTime('completed_at')->nullable();
$table->timestamps();
});

Schema::create('stock_movements', function (Blueprint $table) {
$table->increments('id');
$table->uuid('company_id');
$table->uuid('product_id');
$table->string('entity_type'); //can be order / delivery
$table->string('entity_id'); //can be UUID / String / Integer
$table->string('location_id')->nullable(); // can be warehouse_location / shipment_package / delivery_container
$table->string('action')->default('');
$table->integer('qty')->default(0);
$table->timestamps();
});

我知道它已经有几个月的历史了,但由于没有可接受的解决方案,我发布了我的解决方案
我收到了完全相同的错误消息,但上下文略有不同:

Table::whereIn('fk_id', $arrayOfFkIds)
->delete();

数组包含的值要么是字符串(如ABC1234,如company_id(,要么是整数。

解决方法是当我构建这个数组时,我将所有内容都转换为字符串:

$arrayOfFkIds[] = (string)$parsedArray['stuff_id'];

然后不再出现SQL错误,一切都很顺利。

我认为您缺少引号,因此您的UUID可以被视为字符串类型:

delete from `stock_movements` where `stock_movements`.`entity_id` = 10000005 and `stock_movements`.`entity_id` is not null and `stock_movements`.`company_id` = '8b11050c-612c-4922-8b34-d04d579e02a9'

company_id的值被视为数字/双精度(在任何情况下都不是字符串(,因此您可能忘记在将其放入查询之前将其转换为字符串。

如果要比较的变量没有值,也可能出现此错误消息。

例如:$id = ''User::where('id', $id)

我在搜索代码中相同错误的解决方案时遇到了这个问题,我注意到正在处理的变量没有值,当变量开始获得适当的值时,错误得到了解决,代码运行良好。

这就是我得到的

SQLSTATE[22007]:无效的日期时间格式:1292截断了不正确的DOUBLE值:'s'

在我的案例中,问题是类别列的类型是Int(11(,在WHERE中我使用了字符串而不是数字,然后它返回了那个错误,我使用了PDO,数据库中没有准备好的日期时间,所以在我的例子中,这个错误与日期时间无关。

在制作WHERE category = 1时解决

当我试图通过Eloquent作用域删除Models时,我刚刚遇到了完全相同的问题。

这导致了与上述完全相同的错误:

return $query->whereNotIn('remote_status', $this->typeDeletedStatuses)

类型铸造的解决方案对我的口味来说太脆了,不够明显。因此,经过一番尝试和错误之后,我分离了intstring状态,并重建了如下查询:

return $query->whereNotIn('remote_status', $this->typeDeletedStatuses)
->orWhereIntegerNotInRaw('remote_status', $this->typeIntDeletedStatuses);

现在,范围按预期工作。

最新更新