From d9e28fb5a97468a502fd3d5fb94c996a45daa8d0 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Thu, 25 May 2017 22:46:35 +0200 Subject: [PATCH] Add coelesce in income query. --- accountant/models/accounts.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/accountant/models/accounts.py b/accountant/models/accounts.py index 3f63fa5..d7640d8 100644 --- a/accountant/models/accounts.py +++ b/accountant/models/accounts.py @@ -56,17 +56,23 @@ class Account(db.Model): def income(self, begin, end): """Return the income for a specific time period.""" query = db.session.query( - db.func.sum( - Operation.value - ).filter( - db.func.sign(Operation.value) == -1 + db.func.coalesce( + db.func.sum( + Operation.value + ).filter( + db.func.sign(Operation.value) == -1 + ), 0 ).label("expenses"), - db.func.sum( - Operation.value - ).filter( - db.func.sign(Operation.value) == 1 + db.func.coalesce( + db.func.sum( + Operation.value + ).filter( + db.func.sign(Operation.value) == 1 + ), 0 ).label("revenues"), - db.func.sum(Operation.value).label("income") + db.func.coalesce( + db.func.sum(Operation.value), 0 + ).label("income") ).filter( Operation.account_id == self.id, )