将空字符串转换为 Null – 永夜 https://www.shuijingwanwq.com 没有不值得去解决的问题,也没有不值得去学习的技术! Sun, 23 Jun 2024 04:01:39 +0000 zh-Hans hourly 1 https://wordpress.org/?v=7.0 在 Laravel 9 中执行数据库迁移报错:SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: ‘0’ for column ” at row -1 https://www.shuijingwanwq.com/2024/06/23/8669/ https://www.shuijingwanwq.com/2024/06/23/8669/#respond Sun, 23 Jun 2024 04:01:39 +0000 https://www.shuijingwanwq.com/?p=8669 浏览量: 49 1、在 Laravel 9 中执行数据库迁移报错:SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: ‘0’ for column ” at row -1。如图1
在 Laravel 9 中执行数据库迁移报错:SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: '0' for column '' at row -1

图1



PS E:\wwwroot\object> php artisan migrate

   INFO  Running migrations.

  2024_05_20_152915_update_table_stock_out_sort_type ................................................................................ 13,901ms FAIL

   Illuminate\Database\QueryException

  SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: '0' for column '' at row -1 (SQL: ALTER TABLE table CHANGE stock_out_sort stock_out_sort NUMERIC(36, 0) DEFAULT '0' NOT NULL COMMENT '订单缺货计算排序字段')

  at E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Database\Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

  1   E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Database\Connection.php:545
      PDOException::("SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: '0' for column '' at row -1")

  2   E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Database\Connection.php:545
      PDOStatement::execute()
PS E:\wwwroot\object>


2、将报错的 SQL 复制到 Navicat for MySQL 中执行,仍然报错:1366 – Incorrect DECIMAL value: ‘0’ for column ” at row -1。如图2
将报错的 SQL 复制到 Navicat for MySQL 中执行,仍然报错:1366 - Incorrect DECIMAL value: '0' for column '' at row -1。

图2

3、查看迁移文件的代码实现
<pre class="wp-block-syntaxhighlighter-code">

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('table', function (Blueprint $table) {
            $table->decimal('stock_out_sort', 36, 0)->default(0)->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('table', function (Blueprint $table) {
            $table->string('stock_out_sort', 50)->default('')->change();
        });
    }
};


</pre>
4、仔细检查表中的现有数据,发现字段 stock_out_sort 存在值:空字符串。如图3
仔细检查表中的现有数据,发现字段 stock_out_sort 存在值:空字符串

图3

5、将值:空字符串 手动修改为:99801708945158。然后再次执行数据库迁移,迁移文件执行成功,不再报错。如图4
将值:空字符串 手动修改为:99801708945158。然后再次执行数据库迁移,迁移文件执行成功,不再报错

图4

]]>
https://www.shuijingwanwq.com/2024/06/23/8669/feed/ 0
在 Laravel 6 中,请求的参数为空字符串,后端保存为 null 的排查分析 https://www.shuijingwanwq.com/2022/09/30/7009/ https://www.shuijingwanwq.com/2022/09/30/7009/#respond Fri, 30 Sep 2022 01:50:31 +0000 all()]]> https://www.shuijingwanwq.com/?p=7009 浏览量: 103 1、前端请求的参数为空字符串。如图1
前端请求的参数为空字符串

图1



{
  "image": null,
  "align": "center",
  "title": "Image with text overlay",
  "description": "Use overlay text to give your customers insight into your brand. Select imagery and text that relates to your style and story.",
  "button": {
    "text": "Shop Now",
    "url_object": {
      "ID": 1,
      "children": [],
      "key": 3944,
      "object": "home",
      "object_id": 1,
      "title": "Home",
      "url": "/"
    },
    "url": "/"
  },
  "text_color": "#fff",
  "button_text_color": "#000",
  "button_background_color": "#fff",
  "show_overlay": false,
  "opacity": 0
}


2、后端保存后,title 的值为 null 。


{
  "image": null,
  "align": "center",
  "title": null,
  "description": "Use overlay text to give your customers insight into your brand. Select imagery and text that relates to your style and story.",
  "button": {
    "text": "Shop Now",
    "url_object": {
      "ID": 1,
      "children": [],
      "key": 3944,
      "object": "home",
      "object_id": 1,
      "title": "Home",
      "url": "/"
    },
    "url": "/"
  },
  "text_color": "#fff",
  "button_text_color": "#000",
  "button_background_color": "#fff",
  "show_overlay": false,
  "opacity": 0
}


3、在后端打印所有请求参数,var_dump($request->all());。确认 title 的值已经被转换为 null。如图2
在后端打印所有请求参数,var_dump($request->all());。确认 title 的值已经被转换为 null

图2

4、参考:https://learnku.com/docs/laravel/6.x/validation/5144#6633ca 。默认情况下,在 Laravel 应用的全局中间件堆栈 App\Http\Kernel 类中包含了 ConvertEmptyStringsToNull 中间件。此中间件会将空字符串转换为 Null。 5、但是,如果贸然删除掉此中间件,担心其他功能会受到影响,影响范围很难评估。查看 ConvertEmptyStringsToNull 的实现


    /**
     * Transform the given value.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return mixed
     */
    protected function transform($key, $value)
    {
        return is_string($value) && $value === '' ? null : $value;
    }


6、决定反向转换,在响应给前端时,判断是否为 null ,如果是,则转换为空字符串。最终决定使用 NULL 合并运算符。


// is_null($item['title']) ? '' : $item['title'];
$item['title'] ?? '';


]]>
https://www.shuijingwanwq.com/2022/09/30/7009/feed/ 0