【发布时间】:2025-12-07 15:15:01
【问题描述】:
我正在考虑将 TypeORM 用于一个新项目(TypeScript + express)。
如何定义嵌套关系?
以下是数据结构的示例:
Topic:
{
applicationId: 2,
name: 'test',
recipients: [
{
id: 1,
transports: [
{
id: 1,
templateId: 1,
},
{
id: 2,
templateId: 1,
},
],
},
{
id: 2,
transports: [
{
id: 1,
templateId: 4,
},
],
},
],
startTime: null,
}
Topic 可以有 1 个或多个 Recipients。对于每个 Recipient,有 1 个或多个 Transports。
目前我正在使用单个连接表定义关系:
CREATE TABLE topics_recipients_transports
(
id INT IDENTITY PRIMARY KEY,
topicId INT NOT NULL REFERENCES topics,
recipientId INT NOT NULL REFERENCES recipients,
transportId INT NOT NULL REFERENCES transports,
templateId INT NOT NULL REFERENCES templates,
config NVARCHAR(MAX)
)
GO
CREATE UNIQUE INDEX topics_recipients_transports_unique
ON topics_recipients_transports (topicId, recipientId, transportId)
GO
如您所见,Topic -> Recipient -> Transport 的关系是一对多的,但Transport -> Recipient 的关系依赖于Recipient -> Topic 关联。
【问题讨论】:
-
我也有同样的问题...