Skip to content

Commit

Permalink
fix(core): fix merging of collections loaded via joined strategy
Browse files Browse the repository at this point in the history
Closes #4694
  • Loading branch information
B4nan committed Sep 12, 2023
1 parent faae84e commit b4a0260
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,10 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection

if (map[pk]) {
joinedProps.forEach(hint => {
if (Array.isArray(map[pk][hint.field]) && Array.isArray(item[hint.field])) {
// Sometimes we might join a M:N relation with additional filter on the target entity, and as a result, we get
// the first result with `null` for all target values, which is mapped as empty array. When we see that happen,
// we need to merge the results of the next item.
if (Array.isArray(map[pk][hint.field]) && Array.isArray(item[hint.field]) && map[pk][hint.field].length === 0) {
item[hint.field].forEach((el: T) => map[pk][hint.field].push(el));
}
});
Expand Down

0 comments on commit b4a0260

Please sign in to comment.