diff --git a/packages/core/database/src/tree-repository/adjacency-list-repository.ts b/packages/core/database/src/tree-repository/adjacency-list-repository.ts index 673988e790..78321b91ee 100644 --- a/packages/core/database/src/tree-repository/adjacency-list-repository.ts +++ b/packages/core/database/src/tree-repository/adjacency-list-repository.ts @@ -24,10 +24,14 @@ export class AdjacencyListRepository extends Repository { const childrenKey = collection.treeChildrenField?.name ?? 'children'; - const sql = this.querySQL( - parentNodes.map((node) => node.id), - collection, - ); + const parentIds = parentNodes.map((node) => node[primaryKey]); + + if (parentIds.length == 0) { + this.database.logger.warn('parentIds is empty'); + return parentNodes; + } + + const sql = this.querySQL(parentIds, collection); const childNodes = await this.database.sequelize.query(sql, { type: 'SELECT', @@ -100,7 +104,7 @@ export class AdjacencyListRepository extends Repository { const children = node.getDataValue(childrenKey); - if (children.length === 0) { + if (children && children.length === 0) { node.setDataValue(childrenKey, undefined); } @@ -117,7 +121,7 @@ export class AdjacencyListRepository extends Repository { } private querySQL(rootIds, collection) { - const { treeChildrenField, treeParentField } = collection; + const { treeParentField } = collection; const foreignKey = treeParentField.options.foreignKey; const foreignKeyField = collection.model.rawAttributes[foreignKey].field;