I'm trying to write a query using the Peewee ORM. The query should return all names of Users which have at least one message associated with them. I currently try this as follows:
usersWithAtLeastOneMessage = User.select().where((fn.Count(User.messages) > 0)
for u in usersWithAtLeastOneMessage:
print u.name
in which my models look like this:
class User(db.Model):
name = CharField()
class Message(db.Model):
user = ForeignKeyField(User, related_name='messages')
text = TextField()
This however, gives me the following error: OperationalError: misuse of aggregate function Count()
.
I'm totally lost here though. Does anybody know how I can fix this? All tips are welcome!