兼容数据库为空的情况

This commit is contained in:
zayac 2024-03-31 13:43:19 +08:00
parent 98bf0a4bf4
commit d8ed1bfdd0

View File

@ -2,7 +2,7 @@ from dataclasses import dataclass
from typing import List
from cachetools import TTLCache, cached
from sqlalchemy import String, func
from sqlalchemy import String, func, and_
from sqlalchemy.dialects.mssql import TINYINT
from sqlalchemy.orm import Mapped, mapped_column, relationship
@ -47,7 +47,7 @@ def get_all_users():
def get_user_by_telegram_id(telegram_id: int) -> User:
with db.Session() as session:
user = session.query(User).filter(
User.status == 1 and func.find_in_set(telegram_id, User.telegram_ids) > 0).one()
and_(User.status == 1,func.find_in_set(telegram_id, User.telegram_ids) > 0)).one()
# 过滤出所有状态为可用的账户,并将它们存储在 User 对象中
user.accounts = [account for account in user.accounts if account.status == 1]
user.accounts.sort(key=lambda account: account.type.value)