Python 当我使用with实体查询时,Pydantic没有反序列化Sqlalchemy
我使用这部分代码从数据库中获取数据Python 当我使用with实体查询时,Pydantic没有反序列化Sqlalchemy,python,sqlalchemy,fastapi,pydantic,Python,Sqlalchemy,Fastapi,Pydantic,我使用这部分代码从数据库中获取数据 class OrmModel(BaseModel): class Config: orm_mode = True class PruebaVozFila(OrmModel): id:int id_origen:int id_destino:int hora_programada:str hora_ejecutada:str exitoso:bool estado:str
class OrmModel(BaseModel):
class Config:
orm_mode = True
class PruebaVozFila(OrmModel):
id:int
id_origen:int
id_destino:int
hora_programada:str
hora_ejecutada:str
exitoso:bool
estado:str
@router.get("/pruebas-voz",response_model=PruebaVozFila)
def obtener_pruebas_voz(
*,
db: Session = Depends(deps.get_db),
current_user: models.User = Depends(deps.get_current_active_user),
search:Search = Depends(Search)
) -> Any:
temporal_query = db.query(PruebaVoz).filter().with_entities(
PruebaVoz.id,
PruebaVoz.id_origen,
PruebaVoz.id_destino,
PruebaVoz.hora_programada,
PruebaVoz.hora_ejecutada,
PruebaVoz.exitoso,
PruebaVoz.estado,
).first()
return jsonable_encoder(temporal_query)
因此,当我不使用“with_entities”进行查询时,它可以工作,但如果我添加它,它会抛出以下错误
Pydantic似乎无法在时态查询中找到列或其名称
我将查询打印为SQL并提供以下信息
与“With_实体”
不带“带实体”
正如您在实际查询中看到的,参数是相同的。您认为发生了什么。尝试此
response\u model=List[PruebaVozFila]
trued response\u model=List[PruebaVozFila],但是显示了以下错误:backend\u 1 | response->0->id backend\u 1 |字段必填(type=value\u error.缺失)我认为Pydantic没有从结果集中得到正确的值。即使我使用class Config:orm_mode=True为什么不在PruebaVozFila
class内部使用Config?class OrmModel(BaseModel):class Config:orm_mode=True class PruebaVozFila(OrmModel):id:int-id\u-origen:int-id\u-destino:int-hora\u-programmada:str-hora\u-ejecutada:str-exitoso:bool-estado:str orm模型用作PruebaVozFila的父类,因此会继承配置类,并使用self.data=jsonable\u编码器将其修复([row\u-class.from\u-orm(row)用于所有项目中的行])from\u orm似乎将类变量用作dict,因此它现在可以反序列化尝试此response\u model=List[PruebaVozFila]
trued response\u model=List[PruebaVozFila],但显示了以下错误:backend\u 1 | response->0->id backend\u 1 |字段必需(type=value\u error.missing)我认为Pydantic没有从结果集中得到正确的值。即使我使用class Config:orm_mode=True为什么不在PruebaVozFila
class内部使用Config?class OrmModel(BaseModel):class Config:orm_mode=True class PruebaVozFila(OrmModel):id:int-id\u-origen:int-id\u-destino:int-hora\u-programmada:str-hora\u-ejecutada:str-exitoso:bool-estado:str orm模型用作PruebaVozFila的父类,因此会继承配置类,并使用self.data=jsonable\u编码器将其修复([row\u-class.from\u-orm(row)用于所有项目中的行])from_orm似乎将类变量用作dict,因此现在可以反序列化
SELECT pruebavoz.id AS pruebavoz_id, pruebavoz.id_origen AS pruebavoz_id_origen, pruebavoz.id_destino AS pruebavoz_id_destino, pruebavoz.hora_programada AS pruebavoz_hora_programada, pruebavoz.hora_ejecutada AS pruebavoz_hora_ejecutada, pruebavoz.exitoso AS pruebavoz_exitoso, pruebavoz.estado AS pruebavoz_estado
SELECT pruebavoz.id AS pruebavoz_id, pruebavoz.id_origen AS pruebavoz_id_origen, pruebavoz.id_destino AS pruebavoz_id_destino, pruebavoz.hora_programada AS pruebavoz_hora_programada, pruebavoz.hora_ejecutada AS pruebavoz_hora_ejecutada, pruebavoz.exitoso AS pruebavoz_exitoso, pruebavoz.estado AS pruebavoz_estado, pruebavoz.usuario_ingreso AS pruebavoz_usuario_ingreso, pruebavoz.creado AS pruebavoz_creado, pruebavoz.actualizado AS pruebavoz_actualizado