grouping by some not-pk column requires the pk in the group list
See original GitHub issueIssue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[x] mssql
[ ] mysql
/ mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[ ] @next
[x] 0.2.19
(or put your version here)
Steps to reproduce or a small repository showing the problem:
TLDR;
- create a simple table with
id
and some other column - use querybuilder to select the other column
- add a
groupby
pointing to thesome other column
- see the error
QueryFailedError: Error: Column 'Podcast.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Long version
hi! i’m new on typeorm and i giving it a chance. Its an awesome project!
This code
await getRepository(Podcast)
.createQueryBuilder('p')
.select('p.season')
.getMany();
generates this result
[ { season: 1 }, { season: 1 } ]
and adding a simple group by
await getRepository(Podcast)
.createQueryBuilder('p')
.select('p.season')
.groupBy('p.season')
.getMany();
produces this error
QueryFailedError: Error: Column 'Podcast.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
if i add the requested id
await getRepository(Podcast)
.createQueryBuilder('p')
.select('p.season')
.groupBy('p.season')
.addGroupBy('p.id')
.getMany();
the result is
[ { season: 1 }, { season: 1 } ]
the error is gone but the result is not what i expected, because i just want to group by season
and should get one single item (because both are from the same season
).
Thanks in advance
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:5 (2 by maintainers)
Try Using
getRawMany
and create the query builder from the connection or manager - not the repository.You’re trying to map back into the podcast object something that’s not a podcast. The grouped data doesn’t map back into your entity. To do the mapping we need the primary key which is why it gets selected.
The reason why this works in MySQL has to do with the way MySQL handles keys that are not in the group by. Postgres is stricter and does not allow any of this. You can see what’s up with the generated query.
Suffice it to say - this isn’t a bug - more of a support request
For questions, please check out the community slack or check TypeORM’s documentation page on other support avenues - cheers!