雄辩删除 - SQLSTATE[22007]: 无效的日期时间格式:1292 截断不正确的DOUBLE值:

5

我遇到了这个错误:

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();
    });
5个回答

16

我知道这篇文章已经发布几个月了,但鉴于没有被广泛采纳的解决方案,因此我将我的方案发布出来。
我遇到了完全相同的错误信息,只是上下文略有不同:

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

该数组包含的值可以是字符串(例如“ABC1234”,就像您的公司ID一样)或整数。

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

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

然后就没有SQL错误了,一切都顺畅运行。


4

我认为您缺少引号,这样您的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的值被视为数字/双精度浮点型(但绝不是字符串),因此在将其放入查询之前,您可能已经忘记将其转换为字符串。

2
这个错误信息也可能出现在您比较的变量没有值的情况下。
例如:$id = ''User::where('id', $id)
我在寻找解决我代码中同样的错误时遇到了这个问题,我注意到我正在处理的变量没有值,当变量开始获得适当的值时,错误被解决,代码也正常工作。

0

这是我得到的

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

在我的情况下,问题是类别列的类型为Int(11),而在WHERE子句中我使用了字符串而不是数字,然后返回该错误,我使用了带有准备的PDO并且数据库中没有日期时间,因此这个错误与我的日期时间无关。

当使用 WHERE category = 1 时解决。


-1

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

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

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

类型转换的解决方案对我来说太脆弱了,不够明显。 因此,在一些试错之后,我将 intstring 状态分开,并重新构建了查询:

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

现在作用域按预期工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接