我正试图通过遵循正式的fastapi文档,使用python、fastapi和异步sqlalchemy构建一个简单的作业板 ID
从数据库里,它一直给我这个错误 AttributeError: 'job_board' object has no attribute 'query'
当我撞到楼梯时 "/get/{id}"
终点。
希望以下是最小的可复制代码段:
schemas/jobs.py
from typing import Optional
from pydantic import BaseModel
from datetime import date, datetime
class JobBase(BaseModel):
title: Optional[str] = None
company_name: Optional[str] = None
company_url: Optional[str] = None
location: Optional[str] = "remote"
description: Optional[str] = None
date_posted: Optional[date] = datetime.now().date()
class JobCreate(JobBase):
title: str
company_name: str
location: str
description: str
class ShowJob(JobBase):
title: str
company_name: str
company_url: Optional[str]
location: str
date_posted: date
description: str
class Config():
orm_mode = True
routes/route_jobs.py
from fastapi import APIRouter, HTTPException, status
from fastapi import Depends
from sqlalchemy.orm.session import Session
from db.repository.job_board_dal import job_board
from db.models.jobs import Job as model_job
from schemas.jobs import JobCreate, ShowJob
from db.repository.job_board_dal import Job
from depends import get_db
router = APIRouter()
@router.post("/create-job",response_model=ShowJob)
async def create_user(Job: JobCreate, jobs: Job = Depends(get_db)):
owner_id = 1
return await jobs.create_new_job(Job, owner_id)
@router.get("/get/{id}")
def retreive_job_by_id(id:int, session: Session = Depends(get_db)):
#print(type(session))
job_id = job_board.retrieve_job(session, id=id)
if not job_id:
HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail=f"Job with id {id} does not exist")
return job_id
db/repository/job_board_dal.py
from sqlalchemy.orm import Session
from schemas.users import UserCreate
from schemas.jobs import JobCreate
from db.models.users import User
from db.models.jobs import Job
from core.hashing import Hasher
class job_board():
def __init__(self, db_session: Session):
self.db_session = db_session
async def register_user(self, user: UserCreate):
new_user = User(username=user.username,
email=user.email,
hashed_password=Hasher.get_password_hash(user.password),
is_active = False,
is_superuser=False
)
self.db_session.add(new_user)
await self.db_session.flush()
return new_user
async def create_new_job(self, job: JobCreate, owner_id: int):
new_job = Job(**job.dict(), owner_id = owner_id)
self.db_session.add(new_job)
await self.db_session.flush()
return new_job
def retrieve_job(db: Session, id:int):
item = db.query(Job).filter(Job.id == id).first()
return item
那就看你了
from db.session import async_session
from db.repository.job_board_dal import job_board
async def get_db():
async with async_session() as session:
async with session.begin():
yield job_board(session)
我确实尝试了很多东西,甚至尝试使用一个单独的异步会话来检索,该会话还为我提供了attributeerror:“asyncsession”对象没有属性“query”。任何帮助都将不胜感激。
1条答案
按热度按时间bksxznpy1#
get_db
不返回会话,它返回作业板的示例。所以当你这么做的时候retreive_job_by_id(id:int, session: Session = Depends(get_db))
您正在将会话设置为job_board类。所以当你打电话的时候job_id = job_board.retrieve_job(session, id=id)
您正在通过会话,该会话是job_board的一个示例。所以当你这么做的时候item = db.query(Job)
db这里是job_board的一个示例,job_board没有一个名为query的方法。相反,您可能只想更新retrieve job以利用job_board类示例访问其会话。在您的路由器中,您可以使其更可读