我有更多的桌子连接在一起:
module.exports = function (sequelize, DataTypes) { class Document extends AT_ModelDb { static associate(models) { // define association here models.Document.belongsTo (models.DocumentType ,{as: 'type' , foreignKey : 'type_id' , targetKey : "id" }); models.Document.belongsTo (models.DocumentStatus ,{as: 'status' , foreignKey : 'status_id' , targetKey : "id" }); models.Document.belongsTo (models.Document ,{as: 'documentLinked' , foreignKey : "documentLinked_id" , targetKey : 'id' }); models.Document.belongsTo (models.DocumentBatch ,{as: 'batch' , foreignKey : "batch_id" , targetKey : 'id' }); models.Document.belongsTo (models.Project ,{as: 'project' , foreignKey : "project_id" , targetKey : 'id' }); models.Document.belongsTo (models.Budget ,{as: 'budget' , foreignKey : 'budget_id' , targetKey : "id" }); models.Document.hasMany (models.Document_AircraftConfig ,{as: 'documentsAircraftConfigs' , foreignKey : "document_id" , targetKey : 'id' }); models.Document.hasMany (models.Attachment ,{as: 'attachments' , foreignKey : "document_id" , targetKey : 'id' }); models.Document.hasMany (models.DocumentNote ,{as: 'notes' , foreignKey : "document_id" , targetKey : 'id' });
当我询问sequelize.findAll时,我想要有,例如,关于我的主表的15个项目(此处为文档)
当我请求sequelize.find时,我希望在我的主表(此处为文档)上有15个元素,无论选择和连接检索的行数如何。
因此“select”的“limit”子句是不合适的。
我在 PostgreSql 中找到了一种方法:
select *
from (
SELECT dense_rank() over (order by "Document"."id") as "idx","Document"."type_id", "Document"."reference", "Document"."title", "Document"."status_id", "Document"."ata_code", "Document"."ataAH_code", "Document"."issue", "Document"."lead_code", "Document"."documentLinked_id", "Document"."batch_id", "Document"."project_id", "Document"."creationDateAH", "Document"."statusAH", "Document"."budget_id", "Document"."toBeAnalysed", "Document"."createdBy_code", "Document"."updatedBy_code", "Document"."id", "Document"."deleted", "Document"."createdAt", "Document"."updatedAt"
,"criterias"."code" AS "criterias.code", "criterias"."title" AS "criterias.title", "criterias"."createdBy_code" AS "criterias.createdBy_code", "criterias"."updatedBy_code" AS "criterias.updatedBy_code", "criterias"."id" AS "criterias.id",
[...]
FROM "Documents" AS "Document"
LEFT OUTER JOIN ( "Documents_Criterias" AS "criterias->Document_Criteria" INNER JOIN "Criterias" AS "criterias" ON "criterias"."id" = "criterias->Document_Criteria"."criteria_id") ON "Document"."id" = "criterias->Document_Criteria"."document_id"
LEFT OUTER JOIN "Documents_AircraftConfigs" AS "documentsAircraftConfigs" ON "Document"."id" = "documentsAircraftConfigs"."document_id"
LEFT OUTER JOIN "Document_Notes" AS "notes" ON "Document"."id" = "notes"."document_id"
LEFT OUTER JOIN "Document_Status" AS "status" ON "Document"."status_id" = "status"."id"
LEFT OUTER JOIN "Document_Types" AS "type" ON "Document"."type_id" = "type"."id"
LEFT OUTER JOIN "Projects" AS "project" ON "Document"."project_id" = "project"."id"
LEFT OUTER JOIN "Budgets" AS "budget" ON "Document"."budget_id" = "budget"."id"
LEFT OUTER JOIN "Documents" AS "documentLinked" ON "Document"."documentLinked_id" = "documentLinked"."id"
LEFT OUTER JOIN "Document_Batches" AS "batch" ON "Document"."batch_id" = "batch"."id"
WHERE "status"."code" < '25'
AND "Document"."deleted" = false
) as "docs"
where "idx" <= 15
但是如何让 Sequelize 给我这个 sql 命令。
我认为将此功能整合到sequelize中是明智的,例如通过将参数设置为findAll,与“限制”不同,这将使sequelize理解我们只希望在主表上进行限制。并且sequelize将生成一个与当前生成的不同的 sql 顺序。
示例:limitOnMainTable:15
但也许它已经存在了,因为这个功能在分页时非常有用。
您有解决方案可以提供给我吗?
您可以尝试
subQuery: true
,它将限制应用于内部查询(主表)。
例如,假设
Document
为主表,DocumentType
为子表。以下代码
const result = await Document.findAll({
include: DocumentType,
limit: 2,
subQuery: true
});
将创建一个像这样的查询。
SELECT
"Documents".*,
"DocumentTypes"."id" AS "DocumentTypes.id",
...
FROM
(
SELECT
"Documents"."id",
...
FROM
"Documents" AS "Documents"
WHERE
("Documents"."deletedAt" IS NULL)
LIMIT
2
) AS "Documents"
LEFT OUTER JOIN "DocumentTypes" AS "DocumentTypes" ON "Documents"."id" = "DocumentTypes"."DocumentId"
AND (
"DocumentTypes"."deletedAt" IS NULL
);
limit
应用于主表并获取所有关联的子表的记录。