Explorar el Código

fix: 移除 graph_nodes.user_id 外键约束

- 自动 NER 提取时没有用户上下文,user_id 可为 null
- 移除代码中 userId 默认值 "system"
- 更新 graph_tables.sql 注释掉 user_id 外键
- 添加 fix_graph_nodes_fk.sql 用于修复已有数据库

服务器执行:
PGPASSWORD=123123 psql -U lingyue -d lingyue_zhibao -h localhost \
  -f backend/sql/fix_graph_nodes_fk.sql
何文松 hace 1 mes
padre
commit
77b144e137

+ 1 - 1
backend/graph-service/src/main/java/com/lingyue/graph/service/GraphNerService.java

@@ -91,7 +91,7 @@ public class GraphNerService {
             GraphNode node = new GraphNode();
             node.setId(UUID.randomUUID().toString().replace("-", ""));
             node.setDocumentId(documentId);
-            node.setUserId(userId != null ? userId : "system");
+            node.setUserId(userId);  // 可为 null,自动 NER 提取时没有用户上下文
             node.setName(getStringValue(entity, "name"));
             node.setType(getStringValue(entity, "type", "other").toLowerCase());
             node.setValue(getStringValue(entity, "value"));

+ 1 - 1
backend/graph-service/src/main/java/com/lingyue/graph/service/GraphNodeService.java

@@ -41,7 +41,7 @@ public class GraphNodeService {
         GraphNode node = new GraphNode();
         node.setId(UUID.randomUUID().toString().replace("-", ""));
         node.setDocumentId(request.getDocumentId());
-        node.setUserId(request.getUserId() != null ? request.getUserId() : "system");
+        node.setUserId(request.getUserId());  // 可为 null
         node.setName(request.getName());
         node.setType(request.getType());
         node.setValue(request.getValue());

+ 16 - 0
backend/sql/fix_graph_nodes_fk.sql

@@ -0,0 +1,16 @@
+-- 修复 graph_nodes 表的外键约束问题
+-- user_id 不再需要外键约束,因为自动 NER 提取时没有用户上下文
+
+-- 删除 user_id 的外键约束
+ALTER TABLE graph_nodes DROP CONSTRAINT IF EXISTS fk_graph_nodes_user;
+
+-- 确认删除成功
+SELECT 
+    conname AS constraint_name,
+    conrelid::regclass AS table_name
+FROM pg_constraint 
+WHERE conrelid = 'graph_nodes'::regclass 
+  AND contype = 'f';
+
+-- 完成
+SELECT 'graph_nodes user_id 外键约束已移除' AS status;

+ 8 - 5
backend/sql/graph_tables.sql

@@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS graph_nodes (
 );
 
 -- 添加外键约束(如果关联表存在)
+-- 注意:user_id 不添加外键约束,因为自动 NER 提取时可能没有用户上下文
 DO $$
 BEGIN
     IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'documents') THEN
@@ -30,11 +31,13 @@ BEGIN
             FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE;
     END IF;
     
-    IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'users') THEN
-        ALTER TABLE graph_nodes DROP CONSTRAINT IF EXISTS fk_graph_nodes_user;
-        ALTER TABLE graph_nodes ADD CONSTRAINT fk_graph_nodes_user
-            FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
-    END IF;
+    -- user_id 外键约束已移除,允许任意值或 NULL
+    -- 如需恢复,取消下面注释:
+    -- IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'users') THEN
+    --     ALTER TABLE graph_nodes DROP CONSTRAINT IF EXISTS fk_graph_nodes_user;
+    --     ALTER TABLE graph_nodes ADD CONSTRAINT fk_graph_nodes_user
+    --         FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
+    -- END IF;
 END $$;
 
 CREATE INDEX IF NOT EXISTS idx_graph_nodes_document_id ON graph_nodes(document_id);