3

Here's my query, simplified a little (some filters removed):

q = Foo.query.\
    join(Foo.bar).\
    with_entities(Bar.BAZ, Foo.BAT, ...).\
    group_by(Foo.BAT)

This gives me an error:

column "Bar.BAZ" must appear in the GROUP BY clause or be used in an aggregate function

It seems like there are questions on stackoverflow dealing with this error already, like this one, but what they're trying to do is sufficiently dissimilar to what I'm trying to do that I'm having a hard time understanding how their solutions apply and so I think it's alright for me to ask about this error again.

All I want is to group by one of the columns on Foo. Previously I was doing this in python, but I'd prefer to do it in postgres because I suspect it's faster.

Community
  • 1
  • 1
Alex Lenail
  • 12,992
  • 10
  • 47
  • 79
  • 1
    But what does it mean to select `Bar.BAZ`? Since you're grouping by `Foo.BAT`, `Bar.BAZ` can have multiple values; how do you intend to combine those different values? – univerio Jun 13 '16 at 21:17
  • Many Foos can belong to one Bar. So each Foo has a single unique Bar. Foo.bar stores that relationship. BAZ is a name for Foo's bar, so Foo.bar.BAZ (Bar.BAZ) has only one value. I'm not sure how to express this... – Alex Lenail Jun 14 '16 at 03:23
  • 1
    In that particular case, you would just add `Bar.BAZ` to the `GROUP BY` list. – univerio Jun 14 '16 at 17:42

0 Answers0