fix: eager load ApiKey.user with selectinload to avoid lazy load in async context
This commit is contained in:
parent
080eec32d1
commit
94ae1af2c2
1 changed files with 4 additions and 1 deletions
|
|
@ -7,6 +7,7 @@ from fastapi import Depends, HTTPException, Security, status
|
|||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from jose import JWTError, jwt
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import selectinload
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.config import settings
|
||||
|
|
@ -68,7 +69,9 @@ async def verify_api_key(raw_key: str, db: AsyncSession) -> User | None:
|
|||
return None
|
||||
prefix = raw_key[:11]
|
||||
result = await db.execute(
|
||||
select(ApiKey).where(ApiKey.key_prefix == prefix, ApiKey.is_active == True)
|
||||
select(ApiKey)
|
||||
.options(selectinload(ApiKey.user))
|
||||
.where(ApiKey.key_prefix == prefix, ApiKey.is_active == True)
|
||||
.join(ApiKey.user)
|
||||
.where(User.is_active == True)
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue