I have a Flask app which displays some simple statistics about the number of books a user entered and how many they've marked as read each month. When testing the query locally on a SQLite3 database everything works fine. However, when I deploy to Heroku, which uses a PostgreSQL database, the statistics come out wrong. The query looks like:
books_group = db.session.query(extract("month", Book.timestamp), func.count(Book.id), func.count(Book.read)).filter(Book.user_id == current_user.id).group_by(extract("month", Book.timestamp)).all()
When running locally the output looks like: [(4, 27, 21)]
where the first number is the month, the second the amount of books entered and the third the amount of books read. When running on Heroku the output is: [(4.0, 10, 10)]
No matter what I do that amount of books entered and the amount of books marked as read is always equal, eventhough the output should be [(4.0, 10, 5)]
My model looks like:
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(140))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
read = db.Column(db.Boolean, default=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
And the data looks like:
+----+--------+-----------------------+------+---------+
| id | title | timestamp | read | user_id |
+----+--------+-----------------------+------+---------+
| 1 | Foo | 17 apr. 2020 13:14:47 | 1 | 1 |
| 2 | Bar | 17 apr. 2020 13:14:47 | 1 | 1 |
| 3 | Baz | 18 apr. 2020 13:14:47 | 0 | 1 |
| 4 | Foobar | 18 apr. 2020 13:14:47 | 1 | 2 |
| 5 | BarBaz | 18 apr. 2020 13:14:47 | 0 | 2 |
| | FooBaz | 19 apr. 2020 13:14:47 | 0 | 1 |
+----+--------+-----------------------+------+---------+
What is going wrong here?
EDIT:
The solution that worked both for SQLite3 and PostgreSQL was:
books_group = db.session.query(extract("month", Book.timestamp), func.count(Book.id), func.sum(cast(Book.read, Integer))).filter(Book.user_id == current_user.id).group_by(extract("month", Book.timestamp)).all()