title: How to bulk save in Fast API using sqlalchemy add_all
body: <p>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?</p>
  <p><strong>code</strong></p>
  <p><em>schemas.py</em></p>
  <pre><code>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
  </code></pre>
  <p><em>crud.py</em></p>
  <pre><code>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()
  </code></pre>
  <p><em>patients.py</em></p>
  <pre><code>
  @router.post(&quot;/patients&quot;)
  async def post_patients(
      patients : List[schemas.PatientInDb],
      db: Session = Depends(get_db),
      
  ):
      patients_count = crud.create_patients(db, patients)
      return {
          &quot;message&quot;:f&quot;New {patients_count} patients created.&quot;
      }
  </code></pre>
  <p><strong>error</strong></p>
  <pre><code>  File &quot;.\app\api\v1\patients.py&quot;, line 45, in post_patients
      patients_count = crud.create_patients(db, patients)
    File &quot;.\app\crud.py&quot;, line 13, in create_patients
      db.add_all(patients)
    File &quot;c:\users\convergytics\miniconda3\envs\test\lib\site-packages\sqlalchemy\orm\session.py&quot;, line 2016, in add_all
      for instance in instances:
    File &quot;c:\users\convergytics\miniconda3\envs\test\lib\typing.py&quot;, line 682, in inner
      return func(*args, **kwds)
    File &quot;c:\users\convergytics\miniconda3\envs\test\lib\typing.py&quot;, line 1107, in __getitem__
      params = tuple(_type_check(p, msg) for p in params)
    File &quot;c:\users\convergytics\miniconda3\envs\test\lib\typing.py&quot;, line 1107, in &lt;genexpr&gt;
      params = tuple(_type_check(p, msg) for p in params)
    File &quot;c:\users\convergytics\miniconda3\envs\test\lib\typing.py&quot;, line 374, in _type_check
      raise TypeError(msg + &quot; Got %.100r.&quot; % (arg,))
  TypeError: Parameters to generic types must be types. Got 0.
  </code></pre>