graph_tables.sql 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. DO $$
  24. BEGIN
  25. IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'documents') THEN
  26. ALTER TABLE graph_nodes DROP CONSTRAINT IF EXISTS fk_graph_nodes_document;
  27. ALTER TABLE graph_nodes ADD CONSTRAINT fk_graph_nodes_document
  28. FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE;
  29. END IF;
  30. IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'users') THEN
  31. ALTER TABLE graph_nodes DROP CONSTRAINT IF EXISTS fk_graph_nodes_user;
  32. ALTER TABLE graph_nodes ADD CONSTRAINT fk_graph_nodes_user
  33. FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
  34. END IF;
  35. END $$;
  36. CREATE INDEX IF NOT EXISTS idx_graph_nodes_document_id ON graph_nodes(document_id);
  37. CREATE INDEX IF NOT EXISTS idx_graph_nodes_user_id ON graph_nodes(user_id);
  38. CREATE INDEX IF NOT EXISTS idx_graph_nodes_type ON graph_nodes(type);
  39. CREATE INDEX IF NOT EXISTS idx_graph_nodes_parent_id ON graph_nodes(parent_id);
  40. CREATE INDEX IF NOT EXISTS idx_graph_nodes_position ON graph_nodes USING GIN(position);
  41. -- ============================================
  42. -- 2. 图关系表(graph_relations)
  43. -- ============================================
  44. -- 存储实体之间的关系
  45. CREATE TABLE IF NOT EXISTS graph_relations (
  46. id VARCHAR(36) PRIMARY KEY,
  47. from_node_id VARCHAR(36) NOT NULL,
  48. to_node_id VARCHAR(36) NOT NULL,
  49. relation_type VARCHAR(50) NOT NULL, -- BELONGS_TO/USES/LOCATED_IN/EXECUTES/MONITORS等
  50. action_type VARCHAR(50),
  51. action_config JSONB,
  52. order_index INTEGER DEFAULT 0,
  53. condition_expr TEXT,
  54. metadata JSONB DEFAULT '{}',
  55. create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  56. update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  57. CONSTRAINT fk_graph_relations_from_node FOREIGN KEY (from_node_id) REFERENCES graph_nodes(id) ON DELETE CASCADE,
  58. CONSTRAINT fk_graph_relations_to_node FOREIGN KEY (to_node_id) REFERENCES graph_nodes(id) ON DELETE CASCADE
  59. );
  60. CREATE INDEX IF NOT EXISTS idx_graph_relations_from_node ON graph_relations(from_node_id);
  61. CREATE INDEX IF NOT EXISTS idx_graph_relations_to_node ON graph_relations(to_node_id);
  62. CREATE INDEX IF NOT EXISTS idx_graph_relations_type ON graph_relations(relation_type);
  63. -- ============================================
  64. -- 创建更新时间触发器函数(如果不存在)
  65. -- ============================================
  66. CREATE OR REPLACE FUNCTION update_updated_at_column()
  67. RETURNS TRIGGER AS $$
  68. BEGIN
  69. NEW.update_time = CURRENT_TIMESTAMP;
  70. RETURN NEW;
  71. END;
  72. $$ LANGUAGE plpgsql;
  73. -- 创建触发器
  74. DROP TRIGGER IF EXISTS update_graph_nodes_updated_at ON graph_nodes;
  75. CREATE TRIGGER update_graph_nodes_updated_at
  76. BEFORE UPDATE ON graph_nodes
  77. FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
  78. DROP TRIGGER IF EXISTS update_graph_relations_updated_at ON graph_relations;
  79. CREATE TRIGGER update_graph_relations_updated_at
  80. BEFORE UPDATE ON graph_relations
  81. FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
  82. -- ============================================
  83. -- 说明
  84. -- ============================================
  85. -- 执行此脚本:
  86. -- psql -U lingyue -d lingyue_zhibao -f graph_tables.sql
  87. --
  88. -- 或在服务器上:
  89. -- psql -U postgres -d lingyue_zhibao -f graph_tables.sql