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

在 Laravel 9 中执行数据库迁移报错:SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: ‘0’ for column ” at row -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
在 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

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理