gpt4 book ai didi

sql - TypeORM:如何定义嵌套数据关系?

转载 作者:行者123 更新时间:2023-12-05 04:08:35 25 4
gpt4 key购买 nike

我正在考虑将 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,
}

一个主题可以有1个或多个收件人。对于每个 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 关联。

最佳答案

嵌套关系可以定义为:

this.topicRepo.find({ relations: ["recipients", "recipients.transports"] });

关于sql - TypeORM:如何定义嵌套数据关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47271623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com