My fastApi app takes a put request from a React frontend. It seems like there's a mismatch between the Boolean type parsed by pydantic and that needed by sqlalchemy. I get the following error message:
raise TypeError("Not a boolean value: %r" % value)
sqlalchemy.exc.StatementError: (builtins.TypeError) Not a boolean value: True
[SQL: UPDATE mvhr_units SET description=%(description)s, elec_efficiency=%(elec_efficiency)s, heat_recovery_eff=%(heat_recovery_eff)s, range_low=%(range_low)s, range_high=%(range_high)s, vent_type=%(vent_type)s, superseded=%(superseded)s WHERE mvhr_units.id = %(mvhr_units_id)s]
[parameters: [{'range_high': (0.0,), 'elec_efficiency': (20.0,), 'range_low': (0.0,), 'description': ('New fan model',), 'heat_recovery_eff': (0.0,), 'superseded': (True,), 'vent_type': ('balanced',), 'mvhr_units_id': 4}]]
react put request:
const processRowUpdate = (newRow) => {
axios.put(`http://localhost:8000/mvhr/`, newRow
).then((response) => {
const updatedRow = { ...response.data, isNew: false };
setRows(rows.map((row) => (row.id === newRow.id ? updatedRow : row)));
setSnackbar({ children: tableName + ' successfully saved', severity: 'success' });
})
};
route:
@app.put("/mvhr/", response_model=schemas.Mvhr)
def update_mvhr(mvhr: schemas.Mvhr, db: Session = Depends(get_db)):
print(mvhr, type(mvhr))
db_mvhr = crud.update_mvhr(db, mvhr=mvhr)
if db_mvhr is None:
raise HTTPException(status_code=404, detail="mvhr not found")
return db_mvhr
...from crud.py:
def update_mvhr(db: Session, mvhr: schemas.Mvhr):
print(print(mvhr))
db_mvhr = db.query(models.Mvhr_units).filter(models.Mvhr_units.id == mvhr.id).first()
db_mvhr.description = mvhr.description,
db_mvhr.elec_efficiency = mvhr.elec_efficiency,
db_mvhr.heat_recovery_eff = mvhr.heat_recovery_eff,
db_mvhr.range_low = mvhr.range_low,
db_mvhr.range_high = mvhr.range_high,
db_mvhr.vent_type = mvhr.vent_type,
db_mvhr.superseded = mvhr.superseded,
db.add(db_mvhr)
db.commit()
db.refresh(db_mvhr)
return db_mvhr
Pydantic schema:
class MvhrBase(BaseModel):
description: str
elec_efficiency: float
heat_recovery_eff: float
range_low: float
range_high: float
vent_type: str
superseded: bool
class MvhrCreate(MvhrBase):
pass
class Mvhr(MvhrBase):
id: int
class Config:
orm_mode = True
The data looks like this when printed from the route:
description='New fan model' elec_efficiency=20.0 heat_recovery_eff=0.0 range_low=0.0 range_high=0.0 vent_type='balanced' superseded=True id=4 <class 'app.schemas.Mvhr'>
Why am I getting this error?