Execute database migration error in Laravel 9: SQLState[HY000]: General error: 1366 Incorrect decimal value:0for columnat row -1
1. Execute the database migration error in Laravel 9: sqlstate[HY000]: General error: 1366 Incorrect decimal value:0for columnat row -1. as shown in Figure 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. Copy the error-reported sql to Navicat for MySQL to execute, but still report the error: 1366 – Incorrect decimal value:0for columnat row -1. as shown in Figure 2
3. Check the code implementation of the migration file
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. Check the existing data in the table carefully, and find that the field stock_out_sort exists value: an empty string. as shown in Figure 3
5. Manually modify the value: empty string to: 99801708945158. Then execute the database migration again, the migration file is executed successfully, and no errors are reported. as shown in Figure 4
![在 Laravel 9 中执行数据库迁移报错:SQLSTATE[HY000]: General error: 1366 Incorrect DECIMAL value: '0' for column '' at row -1](https://www.shuijingwanwq.com/wp-content/uploads/2024/06/1-8.png)


