Reputation: 23
I'm trying bulk add patients to the DB, but I'm running into an error. The goal is to read the data from the body of the request, truncate the data in the table and add the new data. Can someone tell me what I am doing wrong?
code
schemas.py
from pydantic import BaseModel
from typing import Optional
class PatientBase(BaseModel):
ticket_id: str
patient_name: Optional[str] = None
class PatientInDb(PatientBase):
patient_id : str
institute :str
class Config:
orm_mode = True
crud.py
from typing import List
from sqlalchemy.orm import Session
def create_patients(db: Session, patients: List[schemas.PatientInDb] ):
num_of_deleted_rows = db.query(models.Patient).delete()
db.add_all(patients)
db.commit()
return db.query(models.Patient).count()
patients.py
@router.post("/patients")
async def post_patients(
patients : List[schemas.PatientInDb],
db: Session = Depends(get_db),
):
patients_count = crud.create_patients(db, patients)
return {
"message":f"New {patients_count} patients created."
}
error
File ".\app\api\v1\patients.py", line 45, in post_patients
patients_count = crud.create_patients(db, patients)
File ".\app\crud.py", line 13, in create_patients
db.add_all(patients)
File "c:\users\convergytics\miniconda3\envs\test\lib\site-packages\sqlalchemy\orm\session.py", line 2016, in add_all
for instance in instances:
File "c:\users\convergytics\miniconda3\envs\test\lib\typing.py", line 682, in inner
return func(*args, **kwds)
File "c:\users\convergytics\miniconda3\envs\test\lib\typing.py", line 1107, in __getitem__
params = tuple(_type_check(p, msg) for p in params)
File "c:\users\convergytics\miniconda3\envs\test\lib\typing.py", line 1107, in <genexpr>
params = tuple(_type_check(p, msg) for p in params)
File "c:\users\convergytics\miniconda3\envs\test\lib\typing.py", line 374, in _type_check
raise TypeError(msg + " Got %.100r." % (arg,))
TypeError: Parameters to generic types must be types. Got 0.
Upvotes: 0
Views: 5874
Reputation: 135
In the file "patients.py"
Change:
"patients = List[schemas.PatientInDb]"
to
"patients: List[schemas.PatientInDb]"
Reason, " = " is for assigning a default value, " : " is for referencing the type.
Upvotes: 2
Reputation: 638
You got this answer on discord as well, but you're saving pydantic models instead of sqlalchemy models.
Upvotes: 0