Files
leaudit-platform-frontend/database/rename_foreign_key.sql
T
2025-12-05 00:09:32 +08:00

42 lines
1.8 KiB
SQL

-- ========================================
-- 重命名外键约束,使其与代码中的名称一致
-- ========================================
-- 步骤 1: 删除旧的外键约束
ALTER TABLE evaluation_points
DROP CONSTRAINT IF EXISTS fk_evaluation_points_group;
-- 步骤 2: 创建新的外键约束,使用正确的名称
ALTER TABLE evaluation_points
ADD CONSTRAINT fk_evaluation_points_child_group
FOREIGN KEY (evaluation_point_groups_id)
REFERENCES evaluation_point_groups(id)
ON DELETE SET NULL
ON UPDATE CASCADE;
-- 添加注释
COMMENT ON CONSTRAINT fk_evaluation_points_child_group ON evaluation_points
IS '评查点所属规则组外键约束(子分组)';
-- 步骤 3: 验证外键约束(应该是正确的名称)
SELECT
tc.constraint_name AS "约束名",
kcu.column_name AS "列名",
ccu.table_name AS "引用表",
ccu.column_name AS "引用列"
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_name = 'evaluation_points'
AND kcu.column_name IN ('evaluation_point_groups_id', 'evaluation_point_groups_pid')
ORDER BY kcu.column_name;
-- 预期结果:
-- 约束名 | 列名 | 引用表 | 引用列
-- ------------------------------------------|-------------------------------|---------------------------|-------
-- fk_evaluation_points_child_group | evaluation_point_groups_id | evaluation_point_groups | id
-- fk_evaluation_points_parent_group | evaluation_point_groups_pid | evaluation_point_groups | id