Cor*_*ing 9 database postgresql tree comments hierarchy
我正在考虑在我的应用程序中使用PostgreSQL的Ltree模块来帮助处理线程注释.我一直在讨论它用于线程评论.我认为这对于需要更新节点及其子节点的情况有帮助,例如当您想要隐藏注释及其回复时.
我正在考虑它(或类似的东西)如果它与传统的邻接列表("comment_id"/"parent_comment_id")相结合将是有用的.
在开始使用ltree之前,我想知道一些事情:
如果它有任何帮助,这里是我正在考虑的架构:
CREATE TABLE comments (
comment_id SERIAL PRIMARY KEY,
parent_comment_id int REFERENCES comments(comment_id) ON UPDATE CASCADE ON DELETE CASCADE,
thread_id int NOT NULL REFERENCES threads(thread_id) ON UPDATE CASCADE ON DELETE CASCADE,
path ltree NOT NULL,
comment_body text NOT NULL,
hide boolean not null default false
);
Run Code Online (Sandbox Code Playgroud)
ltree使用的"path"列看起来像:
<thread_id>.<parent_comment_id_#1>.<parent_comment_id_#2>.<my_comment_id>
Run Code Online (Sandbox Code Playgroud)
在路径中使用主键有什么问题吗?我应该在路径中包含节点自己的主键吗?如果我这样做,在它上面放一个唯一的索引作为约束是否有意义?
其中一个表的定义:
Table "knowledgebase.section"
Column | Type | Modifiers
----------------------------+--------------------------+-----------------------------------------------------------------------------
section_sid | integer | not null default nextval('knowledgebase.section_section_sid_seq'::regclass)
section | character varying | not null
description | character varying |
path | ltree | not null
is_active | boolean | not null default true
role_sid | integer | not null
last_modified_by | integer | not null
creation_datetime | timestamp with time zone | not null default now()
last_modification_datetime | timestamp with time zone | not null default now()
is_expanded | boolean | not null default false
section_idx | tsvector |
Indexes:
"section_sid_pkey" PRIMARY KEY, btree (section_sid)
"section_section_key" UNIQUE, btree (section)
"idxsection_idx" gist (section_idx)
"path_gist_idx" gist (path)
Foreign-key constraints:
"last_modified_by_fkey" FOREIGN KEY (last_modified_by) REFERENCES "user"."role"(role_sid) ON UPDATE CASCADE ON DELETE RESTRICT
"role_sid_fkey" FOREIGN KEY (role_sid) REFERENCES "user"."role"(role_sid) ON UPDATE CASCADE ON DELETE RESTRICT
Triggers:
section_idx_update BEFORE INSERT OR UPDATE ON knowledgebase.section FOR EACH ROW EXECUTE PROCEDURE tsearch2('section_idx', 'section')
Run Code Online (Sandbox Code Playgroud)
"path"列使用主键作为标签.
该表当前内容的示例(关于主键和"路径"列):
section_sid | path
-------------+-------
53 | 34.53
56 | 56
55 | 29.55
35 | 35
54 | 34.54
37 | 30.37
... | ...
Run Code Online (Sandbox Code Playgroud)