Skip to content

Commit 74ed623

Browse files
fix: regression when fetching has-many and not selecting any fields on a hasone/belongsto relation (#720)
1 parent c98479d commit 74ed623

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

src/services/has-many-getter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { pick } from 'lodash';
12
import Operators from '../utils/operators';
23
import QueryUtils from '../utils/query';
34
import PrimaryKeysManager from './primary-keys-manager';
@@ -35,8 +36,7 @@ class HasManyGetter extends ResourcesGetter {
3536
as: associationName,
3637
scope: false,
3738
required: !!buildOptions.forCount, // Why?
38-
where: options.where,
39-
include: options.include,
39+
...pick(options, ['where', 'include']),
4040
}],
4141
});
4242

src/services/line-stat-getter.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,22 @@ ${groupByDateFieldFormated}), 'yyyy-MM-dd 00:00:00')`);
201201
const queryOptions = new QueryOptions(model, { includeRelations: true });
202202
await queryOptions.filterByConditionTree(filters, timezone);
203203

204-
const { include, where } = queryOptions.sequelizeOptions;
205-
const records = await model.unscoped().findAll({
206-
include: include
207-
? include.map((includeProperties) => ({ ...includeProperties, attributes: [] }))
208-
: undefined,
209-
where,
204+
const sequelizeOptions = {
205+
...queryOptions.sequelizeOptions,
210206
attributes: [getGroupByDateInterval(), getAggregate()],
211207
group: getGroupBy(),
212208
order: getOrder(),
213209
raw: true,
214-
});
210+
};
211+
212+
// do not load related properties
213+
if (sequelizeOptions.include) {
214+
sequelizeOptions.include = sequelizeOptions.include.map(
215+
(includeProperties) => ({ ...includeProperties, attributes: [] }),
216+
);
217+
}
218+
219+
const records = await model.unscoped().findAll(sequelizeOptions);
215220

216221
return {
217222
value: fillEmptyDateInterval(

src/services/pie-stat-getter.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,8 @@ function PieStatGetter(model, params, options) {
8585
const queryOptions = new QueryOptions(model, { includeRelations: true });
8686
await queryOptions.filterByConditionTree(filters, timezone);
8787

88-
const { include, where } = queryOptions.sequelizeOptions;
89-
const records = await model.unscoped().findAll({
90-
include: include
91-
? include.map((includeProperties) => ({ ...includeProperties, attributes: [] }))
92-
: undefined,
93-
where,
88+
const sequelizeOptions = {
89+
...queryOptions.sequelizeOptions,
9490
attributes: [
9591
[options.Sequelize.col(groupByField), ALIAS_GROUP_BY],
9692
[
@@ -101,7 +97,15 @@ function PieStatGetter(model, params, options) {
10197
group: getGroupBy(),
10298
order: [[options.Sequelize.literal(ALIAS_AGGREGATE), 'DESC']],
10399
raw: true,
104-
});
100+
};
101+
102+
if (sequelizeOptions.include) {
103+
sequelizeOptions.include = sequelizeOptions.include.map(
104+
(includeProperties) => ({ ...includeProperties, attributes: [] }),
105+
);
106+
}
107+
108+
const records = await model.unscoped().findAll(sequelizeOptions);
105109

106110
return { value: formatResults(records) };
107111
};

test/databases.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,6 +2990,34 @@ const HasManyDissociator = require('../src/services/has-many-dissociator');
29902990
});
29912991
});
29922992

2993+
describe('request on the has-many getter without relations', () => {
2994+
it('should generate a valid SQL query', async () => {
2995+
expect.assertions(1);
2996+
const { models, sequelizeOptions } = initializeSequelize();
2997+
const params = {
2998+
recordId: 100,
2999+
associationName: 'addresses',
3000+
fields: {
3001+
address: 'line,zipCode,city,country',
3002+
},
3003+
page: { number: '1', size: '20' },
3004+
timezone: 'Europe/Paris',
3005+
};
3006+
try {
3007+
await new HasManyGetter(
3008+
models.user,
3009+
models.address,
3010+
sequelizeOptions,
3011+
params,
3012+
)
3013+
.perform();
3014+
expect(true).toBeTrue();
3015+
} finally {
3016+
connectionManager.closeConnection();
3017+
}
3018+
});
3019+
});
3020+
29933021
describe('request on the has-many-getter with a sort on an attribute', () => {
29943022
it('should generate a valid SQL query', async () => {
29953023
expect.assertions(1);

0 commit comments

Comments
 (0)