question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

grouping by some not-pk column requires the pk in the group list

See original GitHub issue

Issue 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;

  1. create a simple table with id and some other column
  2. use querybuilder to select the other column
  3. add a groupby pointing to the some other column
  4. 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:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
imnotjamescommented, Jul 3, 2021

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.

0reactions
imnotjamescommented, Jul 3, 2021

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can I select all fields when grouping by primary key ...
When GROUP BY is present, or any aggregate functions are present, it is not valid for the SELECT list expressions to refer to...
Read more >
Use grouping to modify a list or library view
Grouping lets you organize your SharePoint data based on one or two columns, and can be combined with filters or sorting.
Read more >
mysql - Why is it not sufficient to group by a primary key?
For all rows within the items table, grouping by the PK is sufficient. As I understand it, some RDBMSs require that GROUP BY...
Read more >
SQL GROUP BY clause
When some rows are retrieved from a grouped result against some ... In an SQL statement, suppose you are using GROUP BY, if...
Read more >
7 Common GROUP BY Errors
To correct this type of error, you need to add a GROUP BY meal_category at the end of ... Listing a Column Inside...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found