没有不值得去解决的问题,也没有不值得去学习的技术!

Database Migration Error in Laravel 9: SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: ‘0’ for column ” at row -1

Running a database migration in Laravel 9 throws an error: SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: '0' for column '' at row -1

1. Running a database migration in Laravel 9 throws an error: SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: ‘0’ for column ” at row -1. See Figure 1.

Running a database migration in Laravel 9 throws an error: SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: '0' for column '' at row -1
Figure 1
Plaintext
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->prepare_bindings($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. Copying the failed SQL into Navicat for MySQL to execute still throws an error: 1366 – Incorrect DECIMAL value: ‘0’ for column ” at row -1. See Figure 2.

Copying the failed SQL into Navicat for MySQL to execute still throws an error: 1366 - Incorrect DECIMAL value: '0' for column '' at row -1.
Figure 2

3. Review the code implementation of the migration file.

PHP

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();
        });
    }
};


4. Upon carefully inspecting the existing data in the table, I found that the stock_out_sort field contained an empty string. See Figure 3.

Upon carefully inspecting the existing data in the table, I found that the stock_out_sort field contained an empty string
Figure 3

5. I manually changed the empty string value to 99801708945158. Then I ran the database migration again; the migration file executed successfully and no longer threw an error. See Figure 4.

I manually changed the empty string value to 99801708945158. Then I ran the database migration again; the migration file executed successfully and no longer threw an error
Figure 4

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系