why I got error return [StoredLogRecordDto.fromorm(model[0]) for model in modellist]…
- User
why I got error return [StoredLogRecordDto.from_orm(model[0]) for model in model_list] File "pydantic/main.py", line 579, in pydantic.main.BaseModel.from_orm pydantic.error_wrappers.ValidationError: 5 validation errors for StoredLogRecordDto request_datetime field required (type=value_error.missing) request_url field required (type=value_error.missing) request_method field required (type=value_error.missing) response_datetime field required (type=value_error.missing) response_status_code field required (type=value_error.missing) 2023-05-25 09:05:22,145 INFO sqlalchemy.engine.Engine BEGIN (implicit) 2023-05-25 09:05:22,145 INFO sqlalchemy.engine.Engine SELECT log.id, log.request_datetime, log.request_url, log.request_method, log.response_datetime, log.response_status_code, log.response_error_type, log.response_error_number, log.response_error_code, log.phone_number, log.duration FROM log LIMIT $1::INTEGER 2023-05-25 09:05:22,145 INFO sqlalchemy.engine.Engine [cached since 274.7s ago] (10,) 2023-05-25 09:05:22,147 INFO sqlalchemy.engine.Engine ROLLBACK INFO: 127.0.0.1:49370 - "GET /logging/log?limit=10 HTTP/1.1" 500 Internal Server Error ERROR: Exception in ASGI application Traceback (most recent call last): File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 435, in run_asgi result = await app( # type: ignore[func-returns-value] File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in call return await self.app(scope, receive, send) File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/fastapi/applications.py", line 276, in call await super().call(scope, receive, send) File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/starlette/applications.py", line 122, in call await self.middleware_stack(scope, receive, send) File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in call raise exc File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in call await self.app(scope, receive, _send) File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in call raise exc File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in call await self.app(scope, receive, sender) File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in call raise e File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in call await self.app(scope, receive, send) File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/starlette/routing.py", line 718, in await route.handle(scope, receive, send) File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle await self.app(scope, receive, send) File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/starlette/routing.py", line 66, in app response = await func(request) File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app raw_response = await run_endpoint_function( File "/home/boika/PycharmProjects/logging/venv/lib/python3.10/site-packages/fastapi/routing.py", line 163, in run_endpoint_function return await dependant.call(**values) File "/home/boika/PycharmProjects/logging/src/routes/logging_router.py", line 31, in get_log results_list = await repository.get_logs( File "/home/boika/PycharmProjects/logging/src/db/repository.py", line 41, in get_logs return mapper.map_model_list_to_dto(result.fetchall()) File "/home/boika/PycharmProjects/logging/src/db/mapper.py", line 37, in map_model_list_to_dto return [StoredLogRecordDto.from_orm(model[0]) for model in model_list] File "/home/boika/PycharmProjects/logging/src/db/mapper.py", line 37, in return [StoredLogRecordDto.from_orm(model[0]) for model in model_list] File "pydantic/main.py", line 579, in pydantic.main.BaseModel.from_orm pydantic.error_wrappers.ValidationError: 5 validation errors for StoredLogRecordDto request_datetime field required (type=value_error.missing) request_url field required (type=value_error.missing) request_method field required (type=value_error.missing) response_datetime field required (type=value_error.missing) response_status_code field required (type=value_error.missing)
- Assistant
The error indicates that some fields in the
StoredLogRecordDtoare missing or required. Specifically, therequest_datetime,request_url,request_method,response_datetime, andresponse_status_codefields are required but missing in the database query result.To fix this error, make sure that the required fields are not
nullablein theLogmodel and that they are included in the database query. Also, make sure that theStoredLogRecordDtohas the correct structure that matches the database and includes all required fields.Additionally, check that the
filtervariable inget_logsmethod is properly defined and used. It seems to be missing in the code snippet provided. - User
how to solve
- Assistant
- User
why I need here None class StoredLogRecordDto(BaseLogRecordDto): “”“ DTO that contains response_error_code as three strings. In this form we return the data “”” response_error_type: Optional[str] = None response_error_number: Optional[str] = None response_error_code: Optional[str] = None if i do it here async def get_logs( session: AsyncSession, limit: Optional[int] = 10, request_url: Optional[str] = None, response_status_code: Optional[int] = None, phone_number: Optional[str] = None, ) -> List[StoredLogRecordDto]:
- Assistant
You need to set the
StoredLogRecordDtoclass fields as optional (using the Optional type hint) with a default value ofNonebecause these fields may not be present in every log record in the database.If a log record in the database has a value for
response_error_type,response_error_number, andresponse_error_code, then these values will be returned in the DTO. If a log record does not have one or more of these values, they will be set toNone.By making the fields optional, you can ensure that the DTO object will be created even if some fields are missing in the database record. This also allows you to avoid raising validation errors when mapping from the database model to the DTO.