修复:将 position_authority_logs.user_position_id 改为可空,修复超管发放奖励时报约束违反错误

This commit is contained in:
2026-03-06 16:49:02 +08:00
parent a562564e88
commit 28d9f9ee96

View File

@@ -0,0 +1,59 @@
<?php
/**
* 文件功能:将 position_authority_logs.user_position_id 改为可空
*
* 原字段为 NOT NULL但超级管理员id=1)无职务记录,
* 发放奖励时 user_position_id null,导致约束违反错误。
* 修改为 nullable 后,超管操作写 null,普通职务人员写实际职务 ID。
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* user_position_id 改为可空,兼容超级管理员无职务的情况
*/
public function up(): void
{
Schema::table('position_authority_logs', function (Blueprint $table) {
// 先删除原外键约束,再重新定义为 nullable
$table->dropForeign(['user_position_id']);
$table->unsignedBigInteger('user_position_id')
->nullable()
->change();
$table->foreign('user_position_id')
->references('id')
->on('user_positions')
->nullOnDelete();
});
}
/**
* 回滚:恢复为 NOT NULL(需确保数据中无 null 值)
*/
public function down(): void
{
Schema::table('position_authority_logs', function (Blueprint $table) {
$table->dropForeign(['user_position_id']);
$table->unsignedBigInteger('user_position_id')
->nullable(false)
->change();
$table->foreign('user_position_id')
->references('id')
->on('user_positions')
->cascadeOnDelete();
});
}
};