簡體   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