繁体   English   中英

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

[英]Eloquent Delete - SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value:

我收到此错误:

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的值被视为数字/双company_id值(在任何情况下都不是字符串),因此您可能忘记在将其放入查询之前将其转换为字符串。

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

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

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

在尝试通过 Eloquent 范围删除模型时,我刚刚遇到了完全相同的问题。

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

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

类型铸造的解决方案对我的口味来说太脆了,不够明显。 因此,经过反复试验,我将intstring状态分开,并像这样重建查询:

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

现在范围按预期工作。

这是我得到的

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

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

当 make WHERE category = 1时解决

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM