From cb7de84771ce41f5c4072af7f53037ee7181453e Mon Sep 17 00:00:00 2001 From: ckt1031 <65409152+ckt1031@users.noreply.github.com> Date: Thu, 19 Oct 2023 21:09:23 +0800 Subject: [PATCH] fix: postgres ifnull issue --- model/log.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/model/log.go b/model/log.go index d26da9a2..134bb44c 100644 --- a/model/log.go +++ b/model/log.go @@ -3,8 +3,9 @@ package model import ( "context" "fmt" - "gorm.io/gorm" "one-api/common" + + "gorm.io/gorm" ) type Log struct { @@ -134,7 +135,14 @@ func SearchUserLogs(userId int, keyword string) (logs []*Log, err error) { } func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channel int) (quota int) { - tx := DB.Table("logs").Select("ifnull(sum(quota),0)") + var tx *gorm.DB + + if common.UsingPostgreSQL { + // PostgreSQL does not support the ISNULL function + tx = DB.Table("logs").Select("COALESCE(sum(quota),0)") + } else { + tx = DB.Table("logs").Select("ifnull(sum(quota),0)") + } if username != "" { tx = tx.Where("username = ?", username) }