Jack
Jack

Reputation: 497

Sql-Alchemy group by and filter by a list of ids

I have a Listing table and a Entry table in a mysql database. Each Listing has one or more Entries and I am trying to get the list of all the distinct entries that belong to a list of Listing ids and group them by summing up the score value.

id_list = "a list of Listing ids"

entries = db.session.query(Entry.entry, sum(Entry.score)).filter(Entry.listing_id.in_(id_list)).group_by(Entry.entry).all()

But I am getting this error when I run the above query in Flask:

NotImplementedError: Operator 'getitem' is not supported on this expression

I have tried google but I have not found anything related to my particular case, any ideas what I am doing wrong

Upvotes: 0

Views: 671

Answers (1)

Till
Till

Reputation: 323

As mentioned in my comment, the problem lies with sum.

Try from sqlalchemy import func and then entries = db.session.query(Entry.entry, func.sum(Entry.score))...

Upvotes: 1

Related Questions