graph_tables.sql 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. -- 灵越智报 v2.0 图数据库表结构
  2. -- PostgreSQL 15+
  3. -- 用于 NER 实体识别结果存储
  4. -- ============================================
  5. -- 1. 图节点表(graph_nodes)
  6. -- ============================================
  7. -- 存储从文档中提取的命名实体
  8. CREATE TABLE IF NOT EXISTS graph_nodes (
  9. id VARCHAR(36) PRIMARY KEY,
  10. document_id VARCHAR(36) NOT NULL,
  11. user_id VARCHAR(36), -- 可为空,自动提取时可能没有用户上下文
  12. name VARCHAR(255) NOT NULL,
  13. type VARCHAR(50) NOT NULL, -- text/table/image/number/date/ORG/PERSON/LOC/TIME/DEVICE/PROJECT/METHOD等
  14. value TEXT,
  15. position JSONB, -- {charStart, charEnd, line}
  16. parent_id VARCHAR(36),
  17. level INTEGER DEFAULT 0,
  18. metadata JSONB DEFAULT '{}',
  19. create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  20. update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  21. );
  22. -- 添加外键约束(如果关联表存在)
  23. -- 注意:user_id 不添加外键约束,因为自动 NER 提取时可能没有用户上下文
  24. DO $$
  25. BEGIN
  26. IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'documents') THEN
  27. ALTER TABLE graph_nodes DROP CONSTRAINT IF EXISTS fk_graph_nodes_document;
  28. ALTER TABLE graph_nodes ADD CONSTRAINT fk_graph_nodes_document
  29. FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE;
  30. END IF;
  31. -- user_id 外键约束已移除,允许任意值或 NULL
  32. -- 如需恢复,取消下面注释:
  33. -- IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'users') THEN
  34. -- ALTER TABLE graph_nodes DROP CONSTRAINT IF EXISTS fk_graph_nodes_user;
  35. -- ALTER TABLE graph_nodes ADD CONSTRAINT fk_graph_nodes_user
  36. -- FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
  37. -- END IF;
  38. END $$;
  39. CREATE INDEX IF NOT EXISTS idx_graph_nodes_document_id ON graph_nodes(document_id);
  40. CREATE INDEX IF NOT EXISTS idx_graph_nodes_user_id ON graph_nodes(user_id);
  41. CREATE INDEX IF NOT EXISTS idx_graph_nodes_type ON graph_nodes(type);
  42. CREATE INDEX IF NOT EXISTS idx_graph_nodes_parent_id ON graph_nodes(parent_id);
  43. CREATE INDEX IF NOT EXISTS idx_graph_nodes_position ON graph_nodes USING GIN(position);
  44. -- ============================================
  45. -- 2. 图关系表(graph_relations)
  46. -- ============================================
  47. -- 存储实体之间的关系
  48. CREATE TABLE IF NOT EXISTS graph_relations (
  49. id VARCHAR(36) PRIMARY KEY,
  50. from_node_id VARCHAR(36) NOT NULL,
  51. to_node_id VARCHAR(36) NOT NULL,
  52. relation_type VARCHAR(50) NOT NULL, -- BELONGS_TO/USES/LOCATED_IN/EXECUTES/MONITORS等
  53. action_type VARCHAR(50),
  54. action_config JSONB,
  55. order_index INTEGER DEFAULT 0,
  56. condition_expr TEXT,
  57. metadata JSONB DEFAULT '{}',
  58. create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  59. update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  60. CONSTRAINT fk_graph_relations_from_node FOREIGN KEY (from_node_id) REFERENCES graph_nodes(id) ON DELETE CASCADE,
  61. CONSTRAINT fk_graph_relations_to_node FOREIGN KEY (to_node_id) REFERENCES graph_nodes(id) ON DELETE CASCADE
  62. );
  63. CREATE INDEX IF NOT EXISTS idx_graph_relations_from_node ON graph_relations(from_node_id);
  64. CREATE INDEX IF NOT EXISTS idx_graph_relations_to_node ON graph_relations(to_node_id);
  65. CREATE INDEX IF NOT EXISTS idx_graph_relations_type ON graph_relations(relation_type);
  66. -- ============================================
  67. -- 创建更新时间触发器函数(如果不存在)
  68. -- ============================================
  69. CREATE OR REPLACE FUNCTION update_updated_at_column()
  70. RETURNS TRIGGER AS $$
  71. BEGIN
  72. NEW.update_time = CURRENT_TIMESTAMP;
  73. RETURN NEW;
  74. END;
  75. $$ LANGUAGE plpgsql;
  76. -- 创建触发器
  77. DROP TRIGGER IF EXISTS update_graph_nodes_updated_at ON graph_nodes;
  78. CREATE TRIGGER update_graph_nodes_updated_at
  79. BEFORE UPDATE ON graph_nodes
  80. FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
  81. DROP TRIGGER IF EXISTS update_graph_relations_updated_at ON graph_relations;
  82. CREATE TRIGGER update_graph_relations_updated_at
  83. BEFORE UPDATE ON graph_relations
  84. FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
  85. -- ============================================
  86. -- 说明
  87. -- ============================================
  88. -- 执行此脚本:
  89. -- psql -U lingyue -d lingyue_zhibao -f graph_tables.sql
  90. --
  91. -- 或在服务器上:
  92. -- psql -U postgres -d lingyue_zhibao -f graph_tables.sql