什么包括[].复制:真的在序列化中吗



我正在查看findAll的Sequelize文档,很难理解设置duplicating: true的作用。文件上写着:

将include标记为重复,将阻止使用子查询。

我正在我的模型上的一个include上设置它,结果没有差异,但由于我不明白这个选项的作用,我不确定我应该寻找什么。

我的查询如下:

Sticker.findAndCountAll({
where: { accountId: query.accountId },
attributes: ['id', 'title', 'updatedAt'],
distinct: true,
include: [
{
model: Folder,
attributes: ['id', 'name'],
duplicating: true,
include: [
{ model: Sticker, attributes: ['id'] },
{ model: User, attributes: ['id', 'name', 'email'] }
]
},
{ model: User, attributes: ['id', 'name', 'email'] }
],
offset,
order,
limit
})

我只是在Folderinclude上设置它。

假设我有10个Stickers,5个没有Folder,2个属于一个Folder,3个属于另一个Folder

我正试图获得可能是也可能不是Folder的一部分的Stickers,但如果它们确实属于Folder,我只想要一个";代表性的";从那个Folder,所以对于我的DB中的10个Stickers,我只想要7个结果,5个用于空闲的Stickers,每个Folder1个。顺便说一下,我将Stickers再次包含在Folder中,以便稍后对其进行计数。

我以为duplicating: true可以帮助我实现这一点,但我不明白它的作用。

有人能向我解释一下这个选项是如何转化为查询的吗?这是为了避免对我的关联进行额外的查询吗?如果是,我应该将其设置在所有关联上吗?有可能达到我想要的结果吗?

我找不到duplicating: true是如何工作的。然而,我确实找到了如何按照我想要的方式列出元素。我只需要添加另一个模型来关联它们:

AccountContent.belongsTo('Sticker')
AccountContent.belongsTo('Folder')

通过这种方式,我可以将includeStickersFolders都作为AccountContent在同一查询中的关联。

duplicating: false不会在WHERE CLAUSE中添加subQuery of the include。不设置复制的示例:

SELECT [correspondence].*, [histories].[ID] AS [histories.id], [histories].[CorrespondenceID] AS [histories.correspondenceId], [histories].[UserID] AS [histories.userId], [histories].[Text] AS [histories.text], [histories].[CreatedDTS] AS [histories.createdDate], [histories].[TaskID] AS [histories.taskId], [histories->user].[ID] AS [histories.user.id], [histories->user].[DisplayName] AS [histories.user.displayName], [histories->user->structureEntity].[ID] AS [histories.user.structureEntity.id], [histories->user->structureEntity].[Name] AS [histories.user.structureEntity.name], [comments].[ID] AS [comments.id], [comments].[Body] AS [comments.body], [contacts->organization].[ID] AS [contacts.organization.id], [contacts->organization].[Name] AS [contacts.organization.name], [contacts->employee].[ID] AS [contacts.employee.id], [contacts->employee].[Name] AS [contacts.employee.name], [propertyValues].[Id] AS [propertyValues.id], [propertyValues].[Value] AS [propertyValues.value], [propertyValues].[CorrespondencePropertyId] AS [propertyValues.correspondencePropertyId], [propertyValues->property].[CorrespondenceTypeId] AS [propertyValues.property.correspondenceTypeId], [propertyValues->property].[Id] AS [propertyValues.property.id], [propertyValues->property].[Name] AS [propertyValues.property.name], [tags].[ID] AS [tags.id], [tags].[Name] AS [tags.name], [tags->correspondenceTag].[CorrespondenceID] AS [tags.correspondenceTag.correspondenceId], [tags->correspondenceTag].[TagID] AS [tags.correspondenceTag.tagId], [tags->correspondenceTag].[Notes] AS [tags.correspondenceTag.notes], [participators].[Id] AS [participators.id], [participators].[CorrespondenceId] AS [participators.correspondenceId], [participators].[UserId] AS [participators.userId], [participators].[EntityId] AS [participators.entityId], [participators->entity].[ID] AS [participators.entity.id], [participators->entity].[Name] AS [participators.entity.name], [participators->user].[ID] AS [participators.user.id], [participators->user].[DisplayName] AS [participators.user.displayName] FROM (SELECT [correspondence].[ID] AS [id], [correspondence].[SubjectName] AS [subjectName], [correspondence].[Subject] AS [subject], [correspondence].[StatusID] AS [statusId], [correspondence].[CreatedDTS] AS [createdDate], [correspondence].[ReferenceNumber] AS [referenceNumber], [correspondence].[TypeID] AS [typeId], [correspondence].[PriorityID] AS [priorityId], [contacts].[ID] AS [contacts.id], [contacts].[CorrespondenceId] AS [contacts.correspondenceId], [contacts].[OrganizationId] AS [contacts.organizationId], [contacts].[ContactEmployeeId] AS [contacts.contactEmployeeId], [contacts].[CreatedDTS] AS [contacts.createdDate] FROM [Correspondences] AS [correspondence] INNER JOIN [CorrespondenceContact] AS [contacts] ON [correspondence].[ID] = [contacts].[CorrespondenceId] AND [contacts].[OrganizationId] = N'2144' WHERE ([correspondence].[IsDeleted] = 0 AND [correspondence].[IsArchived] = 0 AND [correspondence].[Published] = 1 AND [correspondence].[TypeID] = 1) AND ( SELECT [CorrespondenceId] FROM [CorrespondencePropertyValue] AS [propertyValues] WHERE ([propertyValues].[Value] = N'SOMR VALUE' AND [propertyValues].[CorrespondenceId] = [correspondence].[ID]) ORDER BY [propertyValues].[Id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY ) IS NOT NULL ORDER BY [correspondence].[CreatedDTS] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY) AS [correspondence] LEFT OUTER JOIN [CorrespondenceHistory] AS [histories] ON [correspondence].[id] = [histories].[CorrespondenceID] LEFT OUTER JOIN [Users] AS [histories->user] ON [histories].[UserID] = [histories->user].[ID] LEFT OUTER JOIN [LKEntityStucture] AS [histories->user->structureEntity] ON [histories->user].[EntityID] = [histories->user->structureEntity].[ID] LEFT OUTER JOIN [CorrespondenceComments] AS [comments] ON [correspondence].[id] = [comments].[CorrespondenceId] AND [comments].[Type] = N'initialComment' LEFT OUTER JOIN [ContactOrganizations] AS [contacts->organization] ON [contacts.organizationId] = [contacts->organization].[ID] LEFT OUTER JOIN [ContactEmployees] AS [contacts->employee] ON [contacts.contactEmployeeId] = [contacts->employee].[ID] INNER JOIN [CorrespondencePropertyValue] AS [propertyValues] ON [correspondence].[id] = [propertyValues].[CorrespondenceId] AND [propertyValues].[Value] = N'SOMR VALUE' LEFT OUTER JOIN [CorrespondenceProperty] AS [propertyValues->property] ON [propertyValues].[CorrespondencePropertyId] = [propertyValues->property].[Id] LEFT OUTER JOIN ( [CorrespondenceTagLookup] AS [tags->correspondenceTag] INNER JOIN [Tags] AS [tags] ON [tags].[ID] = [tags->correspondenceTag].[TagID]) ON [correspondence].[id] = [tags->correspondenceTag].[CorrespondenceID] LEFT OUTER JOIN [CorrespondenceParticipator] AS [participators] ON [correspondence].[id] = [participators].[CorrespondenceId] LEFT OUTER JOIN [LKEntityStucture] AS [participators->entity] ON [participators].[EntityId] = [participators->entity].[ID] LEFT OUTER JOIN [Users] AS [participators->user] ON [participators].[UserId] = [participators->user].[ID] ORDER BY [createdDate] DESC`

参见本行AND (SELECT [CorrespondenceId] FROM [CorrespondencePropertyValue] AS [propertyValues] WHERE ( [propertyValues].[Value] = N'SOME VALUE' AND [propertyValues].[CorrespondenceId] = [correspondence].[ID]) ORDER BY [propertyValues].[Id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY) IS NOT NULL如果将duplicating: false添加到包含选项中,则会被删除。像这样:

SELECT [correspondence].*, [histories].[ID] AS [histories.id], [histories].[CorrespondenceID] AS [histories.correspondenceId], [histories].[UserID] AS [histories.userId], [histories].[Text] AS [histories.text], [histories].[CreatedDTS] AS [histories.createdDate], [histories].[TaskID] AS [histories.taskId], [histories->user].[ID] AS [histories.user.id], [histories->user].[DisplayName] AS [histories.user.displayName], [histories->user->structureEntity].[ID] AS [histories.user.structureEntity.id], [histories->user->structureEntity].[Name] AS [histories.user.structureEntity.name], [comments].[ID] AS [comments.id], [comments].[Body] AS [comments.body], [contacts->organization].[ID] AS [contacts.organization.id], [contacts->organization].[Name] AS [contacts.organization.name], [contacts->employee].[ID] AS [contacts.employee.id], [contacts->employee].[Name] AS [contacts.employee.name], [propertyValues->property].[CorrespondenceTypeId] AS [propertyValues.property.correspondenceTypeId], [propertyValues->property].[Id] AS [propertyValues.property.id], [propertyValues->property].[Name] AS [propertyValues.property.name], [tags].[ID] AS [tags.id], [tags].[Name] AS [tags.name], [tags->correspondenceTag].[CorrespondenceID] AS [tags.correspondenceTag.correspondenceId], [tags->correspondenceTag].[TagID] AS [tags.correspondenceTag.tagId], [tags->correspondenceTag].[Notes] AS [tags.correspondenceTag.notes], [participators].[Id] AS [participators.id], [participators].[CorrespondenceId] AS [participators.correspondenceId], [participators].[UserId] AS [participators.userId], [participators].[EntityId] AS [participators.entityId], [participators->entity].[ID] AS [participators.entity.id], [participators->entity].[Name] AS [participators.entity.name], [participators->user].[ID] AS [participators.user.id], [participators->user].[DisplayName] AS [participators.user.displayName] FROM (SELECT [correspondence].[ID] AS [id], [correspondence].[SubjectName] AS [subjectName], [correspondence].[Subject] AS [subject], [correspondence].[StatusID] AS [statusId], [correspondence].[CreatedDTS] AS [createdDate], [correspondence].[ReferenceNumber] AS [referenceNumber], [correspondence].[TypeID] AS [typeId], [correspondence].[PriorityID] AS [priorityId], [contacts].[ID] AS [contacts.id], [contacts].[CorrespondenceId] AS [contacts.correspondenceId], [contacts].[OrganizationId] AS [contacts.organizationId], [contacts].[ContactEmployeeId] AS [contacts.contactEmployeeId], [contacts].[CreatedDTS] AS [contacts.createdDate], [propertyValues].[Id] AS [propertyValues.id], [propertyValues].[Value] AS [propertyValues.value], [propertyValues].[CorrespondencePropertyId] AS [propertyValues.correspondencePropertyId] FROM [Correspondences] AS [correspondence] INNER JOIN [CorrespondenceContact] AS [contacts] ON [correspondence].[ID] = [contacts].[CorrespondenceId] AND [contacts].[OrganizationId] = 2144 INNER JOIN [CorrespondencePropertyValue] AS [propertyValues] ON [correspondence].[ID] = [propertyValues].[CorrespondenceId] AND [propertyValues].[Value] = N'سري للغاية' WHERE ([correspondence].[IsDeleted] = 0 AND [correspondence].[IsArchived] = 0 AND [correspondence].[Published] = 1 AND [correspondence].[TypeID] = 1) ORDER BY [correspondence].[CreatedDTS] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY) AS [correspondence] LEFT OUTER JOIN [CorrespondenceHistory] AS [histories] ON [correspondence].[id] = [histories].[CorrespondenceID] LEFT OUTER JOIN [Users] AS [histories->user] ON [histories].[UserID] = [histories->user].[ID] LEFT OUTER JOIN [LKEntityStucture] AS [histories->user->structureEntity] ON [histories->user].[EntityID] = [histories->user->structureEntity].[ID] LEFT OUTER JOIN [CorrespondenceComments] AS [comments] ON [correspondence].[id] = [comments].[CorrespondenceId] AND [comments].[Type] = N'initialComment' LEFT OUTER JOIN [ContactOrganizations] AS [contacts->organization] ON [contacts.organizationId] = [contacts->organization].[ID] LEFT OUTER JOIN [ContactEmployees] AS [contacts->employee] ON [contacts.contactEmployeeId] = [contacts->employee].[ID] LEFT OUTER JOIN [CorrespondenceProperty] AS [propertyValues->property] ON [propertyValues.correspondencePropertyId] = [propertyValues->property].[Id] LEFT OUTER JOIN ( [CorrespondenceTagLookup] AS [tags->correspondenceTag] INNER JOIN [Tags] AS [tags] ON [tags].[ID] = [tags->correspondenceTag].[TagID]) ON [correspondence].[id] = [tags->correspondenceTag].[CorrespondenceID] LEFT OUTER JOIN [CorrespondenceParticipator] AS [participators] ON [correspondence].[id] = [participators].[CorrespondenceId] LEFT OUTER JOIN [LKEntityStucture] AS [participators->entity] ON [participators].[EntityId] = [participators->entity].[ID] LEFT OUTER JOIN [Users] AS [participators->user] ON [participators].[UserId] = [participators->user].[ID] ORDER BY [createdDate] DESC;

最新更新