修复scalar_one_or_none相关limit
This commit is contained in:
@@ -16,7 +16,7 @@ from app.utils.exceptions import InsufficientCreditsError
|
||||
async def calc_text_credits(db: AsyncSession, input_tokens: int, output_tokens: int) -> float:
|
||||
"""Calculate text credits based on actual token usage and configurable rate."""
|
||||
result = await db.execute(
|
||||
select(SystemConfig).where(SystemConfig.key == "text_credits_per_1000_tokens")
|
||||
select(SystemConfig).where(SystemConfig.key == "text_credits_per_1000_tokens").limit(1)
|
||||
)
|
||||
config = result.scalar_one_or_none()
|
||||
rate = float(config.value) if config else 1.0
|
||||
@@ -78,6 +78,7 @@ async def calc_video_credits(
|
||||
select(VideoEngine.id)
|
||||
.where(VideoEngine.is_active == True)
|
||||
.order_by(VideoEngine.priority.desc())
|
||||
.limit(1)
|
||||
)
|
||||
engine_id = video_engines_result.scalar_one_or_none()
|
||||
ratio = await _get_credit_ratio(
|
||||
@@ -122,6 +123,7 @@ async def calc_image_credits(
|
||||
select(ImageEngine.id)
|
||||
.where(ImageEngine.is_active == True)
|
||||
.order_by(ImageEngine.priority.desc())
|
||||
.limit(1)
|
||||
)
|
||||
engine_id = image_engines_result.scalar_one_or_none()
|
||||
ratio = await _get_credit_ratio(
|
||||
@@ -148,7 +150,7 @@ async def deduct_credits(
|
||||
) -> User:
|
||||
"""Atomically deduct credits from user. Raises InsufficientCreditsError."""
|
||||
result = await db.execute(
|
||||
select(User).where(User.id == user_id)
|
||||
select(User).where(User.id == user_id).limit(1)
|
||||
)
|
||||
user = result.scalar_one_or_none()
|
||||
if not user or user.credits < amount:
|
||||
@@ -178,7 +180,7 @@ async def add_credits(
|
||||
) -> User:
|
||||
"""Add credits to user."""
|
||||
result = await db.execute(
|
||||
select(User).where(User.id == user_id)
|
||||
select(User).where(User.id == user_id).limit(1)
|
||||
)
|
||||
user = result.scalar_one_or_none()
|
||||
if not user:
|
||||
|
||||
Reference in New Issue
Block a user