QueryBuilder IN and ANY Fail with .where - Postgres
See original GitHub issueIssue type:
[ ] question [x ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[ x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
TypeORM version:
[x ] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
This works:
const sql = 'SELECT * FROM members WHERE '+ integerId + ' = ANY(skill_id_array)';
const membersBySkill = await this.entityManager.query(sql);
// This works: SELECT * FROM members WHERE 2 = ANY(skill_id_array)
Edit: Also this works, although not originally because I set it up like the ‘Any’ example in Find Options in the docs. This needs to be added to the QB doc for those of us who are new to the whole server stack and learning from the docs.
const membersBySkill = await getRepository(Members)
.createQueryBuilder()
.where(':id = ANY (skill_id_array)', {id: integerId})
.getMany();
I’ve tried many setups but the two in the comments below have typical errors. The problem seems to be in the .where with both IN and ANY. Select statements are from my console.log on the server. The var integerId as a value of say 2, is an integer and skill_id_array is a column in the db with values such as {1,2,5}. There are no examples of my use in the docs or elsewhere and I suspect that this setup doesn’t work in TypeORM yet or is a bug.
const membersBySkill = await getRepository(Members)
.createQueryBuilder()
// .select('*') Do not use here. QB needs the entire selection per @pleerock in comments below.
.where(':id IN (skill_id_array)', {id: integerId})
.getMany();
console.log('membersBySkill: ', membersBySkill);
return membersBySkill;
// {id: '{2}'}) -> SELECT * FROM "members" "Members" WHERE $1 IN (skill_id_array) -- PARAMETERS: ["{2}"]
// membersBySkill = [] empty array and no error.
// {id: integerId} -> SELECT * FROM "members" "Members" WHERE $1 IN (skill_id_array) -- PARAMETERS: [2]
// errors: malformed array literal: "2" & Array value must start with "{" or dimension information.
The entity:
@Column('int', { array: true, nullable: true})
skill_id_array: number[];
Issue Analytics
- State:
- Created 5 years ago
- Comments:20 (9 by maintainers)
can be translated into:
or
Thank you for your help! I think QB is more than a wrapper because I can’t use .select * with it in my query because somewhere in the black box of QB it does something different than SQL. This is the kind of mystery that newbies spot quickly and get confused. It eats a lot of time.
IN may work in QB but it is a lot of work. This from my SO post on this topic:
https://stackoverflow.com/questions/50705276/typeorm-postgres-where-any-or-in-with-querybuilder-or-find/50745182#50745182