Why is this search endpoint returning error Unprocessable Entity

  Kiến thức lập trình

I have written this endpoint in fastapi and sqlalchemy

@router.get("/search_diagnosis", status_code=status.HTTP_200_OK)
async def get_diagnosis_by_name(
    user: user_dependency,
    db: db_dependency,
    patient_name: str = Query(None, description="Search by patient name"),
):
    print("*"* 100)
    if user is None:
        raise HTTPException(status_code=401, detail='Authentication Failure')

    profiles = UserProfiles.__table__

    if patient_name:
        where_clause = or_(
            profiles.c.first_name.like(f"%{patient_name}%"),
            profiles.c.last_name.like(f"%{patient_name}%")
        )
    else:
        raise HTTPException(status_code=400, detail="Please enter patient name.")

    stmt = (
        select(
            Patients.patient_id,
            Patients.registration_date,
            Diagnosis.id,
            Diagnosis.diagnosis_description,
            Diagnosis.diagnosis_date,
            Diagnosis.patient_symptoms,
            Diagnosis.doctor_id,
            Users.email,
            Users.phone,
            Users.date_of_birth,
            UserProfiles.first_name,
            UserProfiles.last_name
        )
        .join(Patients, Diagnosis.patient_id  == Patients.patient_id)
        .join(Patients, UserProfiles.user_id == Patients.user_id)
        .join(Users, UserProfiles.user_id == Users.id)
        .where(where_clause)
    )
    results = await db.execute(stmt)
    patients_data = results.fetchall()
    return [
        {
            "patient_id": row.patient_id,
            "registration_date": row.registration_date,
            "email": row.email,
            "phone": row.phone,
            "date_of_birth": row.date_of_birth,
            "first_name": row.first_name,
            "last_name": row.last_name,
        }
            for row in patients_data
    ] or []

but I don’t know why I am getting this error:

{
  "detail": [
    {
      "type": "int_parsing",
      "loc": [
        "path",
        "patient_id"
      ],
      "msg": "Input should be a valid integer, unable to parse string as an integer",
      "input": "search_diagnosis"
    }
  ]
}

Why is it expecting it to be patient id? why the `print("*"*100)` is not outputting on the shell?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT