From 5ef059eb40782f7c9b39c96edf77415d6091d129 Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Tue, 23 Jun 2026 22:58:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=87=8F=E5=B0=91=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=83=85=E6=99=AF=E4=B8=AD=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=A1=A8=E5=AF=B9=20prompt=20=E7=9A=84=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...05_fix_user_scenarios_description.down.sql | 15 +++++++++++ .../005_fix_user_scenarios_description.up.sql | 16 ++++++++++++ check_constraints.sql | 9 +++++++ fix_user_scenarios.sql | 26 +++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 backend/migrations/005_fix_user_scenarios_description.down.sql create mode 100644 backend/migrations/005_fix_user_scenarios_description.up.sql create mode 100644 check_constraints.sql create mode 100644 fix_user_scenarios.sql diff --git a/backend/migrations/005_fix_user_scenarios_description.down.sql b/backend/migrations/005_fix_user_scenarios_description.down.sql new file mode 100644 index 0000000..09a85ee --- /dev/null +++ b/backend/migrations/005_fix_user_scenarios_description.down.sql @@ -0,0 +1,15 @@ +-- 005_fix_user_scenarios_description.down.sql +-- 回滚:恢复原约束(description 必填,prompt 最小长度 50) + +-- 注意:如果表中存在不符合原约束的数据,此回滚会失败 + +-- 1. 恢复 description 为必填 +ALTER TABLE user_scenarios + ALTER COLUMN description SET NOT NULL; + +-- 2. 恢复 prompt 最小长度为 50 +ALTER TABLE user_scenarios + DROP CONSTRAINT IF EXISTS check_prompt_length; + +ALTER TABLE user_scenarios + ADD CONSTRAINT check_prompt_length CHECK (char_length(prompt) >= 50 AND char_length(prompt) <= 2000); diff --git a/backend/migrations/005_fix_user_scenarios_description.up.sql b/backend/migrations/005_fix_user_scenarios_description.up.sql new file mode 100644 index 0000000..31f96c9 --- /dev/null +++ b/backend/migrations/005_fix_user_scenarios_description.up.sql @@ -0,0 +1,16 @@ +-- 005_fix_user_scenarios_description.up.sql +-- 修复 user_scenarios 表约束:description 允许为空,prompt 最小长度改为 10 + +-- 1. 允许 description 为空 +ALTER TABLE user_scenarios + ALTER COLUMN description DROP NOT NULL; + +-- 2. 修复 prompt 长度约束(从 50 改为 10,与代码一致) +ALTER TABLE user_scenarios + DROP CONSTRAINT IF EXISTS check_prompt_length; + +ALTER TABLE user_scenarios + ADD CONSTRAINT check_prompt_length CHECK (char_length(prompt) >= 10 AND char_length(prompt) <= 2000); + +COMMENT ON COLUMN user_scenarios.description IS '简短描述,可选,显示在情景卡片上(允许为空)'; +COMMENT ON COLUMN user_scenarios.prompt IS '角色 System Prompt,定义 AI 行为(10-2000 字符)'; diff --git a/check_constraints.sql b/check_constraints.sql new file mode 100644 index 0000000..f834ab6 --- /dev/null +++ b/check_constraints.sql @@ -0,0 +1,9 @@ +-- 检查 user_scenarios 表的所有约束 +SELECT + con.conname AS constraint_name, + con.contype AS constraint_type, + pg_get_constraintdef(con.oid) AS constraint_definition +FROM pg_constraint con +JOIN pg_class rel ON rel.oid = con.conrelid +WHERE rel.relname = 'user_scenarios' +ORDER BY con.conname; diff --git a/fix_user_scenarios.sql b/fix_user_scenarios.sql new file mode 100644 index 0000000..ab01b43 --- /dev/null +++ b/fix_user_scenarios.sql @@ -0,0 +1,26 @@ +-- 快速修复脚本:修复 user_scenarios 表约束 +-- 在生产 PostgreSQL 容器中执行此文件 + +-- 1. 允许 description 为空 +ALTER TABLE user_scenarios + ALTER COLUMN description DROP NOT NULL; + +-- 2. 修复 prompt 长度约束(从 50 改为 10,与代码一致) +ALTER TABLE user_scenarios + DROP CONSTRAINT IF EXISTS check_prompt_length; + +ALTER TABLE user_scenarios + ADD CONSTRAINT check_prompt_length CHECK (char_length(prompt) >= 10 AND char_length(prompt) <= 2000); + +-- 更新注释 +COMMENT ON COLUMN user_scenarios.description IS '简短描述,可选,显示在情景卡片上(允许为空)'; +COMMENT ON COLUMN user_scenarios.prompt IS '角色 System Prompt,定义 AI 行为(10-2000 字符)'; + +-- 验证修改 +SELECT + con.conname AS constraint_name, + pg_get_constraintdef(con.oid) AS constraint_definition +FROM pg_constraint con +JOIN pg_class rel ON rel.oid = con.conrelid +WHERE rel.relname = 'user_scenarios' + AND con.conname = 'check_prompt_length';