-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
52 lines (47 loc) · 1.83 KB
/
schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- schema.sql
-- Base Schema for Memory Graph
-- Generated on 2024-11-29
-- Enable Foreign Key Constraints
PRAGMA foreign_keys = ON;
-- ===========================
-- Entities Table
-- ===========================
CREATE TABLE IF NOT EXISTS entities (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
entity_type TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- ===========================
-- Observations Table
-- ===========================
CREATE TABLE IF NOT EXISTS observations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
entity_id INTEGER NOT NULL,
relation_id INTEGER, -- Optional: Link to a specific relation
observation TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (entity_id) REFERENCES entities(id),
FOREIGN KEY (relation_id) REFERENCES relations(id)
);
-- ===========================
-- Relations Table
-- ===========================
CREATE TABLE IF NOT EXISTS relations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
from_entity_id INTEGER NOT NULL,
to_entity_id INTEGER NOT NULL,
relation_type INTEGER NOT NULL, -- References relation_types(id)
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (from_entity_id) REFERENCES entities(id),
FOREIGN KEY (to_entity_id) REFERENCES entities(id),
FOREIGN KEY (relation_type) REFERENCES relation_types(id)
);
-- ===========================
-- Indexes for Base Schema
-- ===========================
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
CREATE INDEX IF NOT EXISTS idx_observations_entity_id ON observations(entity_id);
CREATE INDEX IF NOT EXISTS idx_observations_relation_id ON observations(relation_id);
CREATE INDEX IF NOT EXISTS idx_relations_from_to ON relations(from_entity_id, to_entity_id);