Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findByIds with empty array returns value #1118

Closed
icepeng opened this issue Oct 31, 2017 · 4 comments
Closed

findByIds with empty array returns value #1118

icepeng opened this issue Oct 31, 2017 · 4 comments

Comments

@icepeng
Copy link

icepeng commented Oct 31, 2017

If the given id array is empty, we expect an empty array to be returned, but actually all rows of that entity are returned.

I checked that the where clause is missing from the generated query when the Id array is empty. Is this behavior intended?

@NoNameProvided
Copy link
Member

NoNameProvided commented Nov 1, 2017

The issue is that in /src/query-builder/SelectQueryBuilder.ts#L717-L730 it passes the array without checking to the QueryBuilder.createWhereIdsExpression function what as can be seen below, simply generates and where expression for every id in the array. If there are no ids, then no expression will be added.

const whereStrings = ids.map((id, index) => {
id = id instanceof Object ? id : metadata.createEntityIdMap(id);
const whereSubStrings: string[] = [];
metadata.primaryColumns.forEach((primaryColumn, secondIndex) => {
whereSubStrings.push(alias + this.escape(primaryColumn.databaseName) + "=:id_" + index + "_" + secondIndex);
parameters["id_" + index + "_" + secondIndex] = primaryColumn.getEntityValue(id);
});
metadata.parentIdColumns.forEach((parentIdColumn, secondIndex) => {
whereSubStrings.push(alias + this.escape(parentIdColumn.databaseName) + "=:parentId_" + index + "_" + secondIndex);
parameters["parentId_" + index + "_" + secondIndex] = parentIdColumn.getEntityValue(id);
});
return whereSubStrings.join(" AND ");
});
const whereString = whereStrings.length > 1 ? "(" + whereStrings.join(" OR ") + ")" : whereStrings[0];

However, I am not sure how this should be handled as for the fixing also both andWhereInIds and orWhereInIds needed to be taken into account.

@icepeng
Copy link
Author

icepeng commented Nov 1, 2017

I also found that whereString was a problem, but not sure about how to handle when whereStrings.length === 0

@pleerock
Copy link
Member

pleerock commented Nov 1, 2017

Sounds like an issue. Feel free to contribute

@pleerock
Copy link
Member

fixed and will be released in 0.1.5

pleerock pushed a commit that referenced this issue Nov 22, 2017
* master:
  fixes #996
  fixed failing test
  added test for #1145
  added "ON CONFLICT" cause support for #1090
  removed only test
  added support for indices and listeners in embeddeds
  version bump
  added test for #1178
  added test for #1210
  fixes #1201
  removed only test
  fixes #1118
  fixed comments style
  version bump
  fixes #1119 and #1205
  fixes #1110

# Conflicts:
#	package.json
#	src/entity-manager/EntityManager.ts
#	src/migration/MigrationExecutor.ts
#	src/query-builder/InsertQueryBuilder.ts
#	src/repository/BaseEntity.ts
#	src/subscriber/Broadcaster.ts
#	test/github-issues/1178/entity/Post.ts
#	test/github-issues/1178/issue-1178.ts
bbakhrom added a commit that referenced this issue Mar 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants