Reputation: 27
I'm fairly new to this framework, but i'm working on a timesheet application (API) for a small company.
The issue i have is that when i select a time value from mysql i got the following error in the pydantic module:
pydantic.error_wrappers.ValidationError: 2 validation errors for TimeSheetRange
response -> 0 -> total
invalid type; expected time, string, bytes, int or float (type=type_error)
response -> 1 -> total
invalid type; expected time, string, bytes, int or float (type=type_error)
I have tried a lot of workarounds to get the time to string from mysql with to_char()
which also failed, but i rather want to know what the issue is here as i would like to use the data type time...
schemas.py
from typing import List, Optional
from datetime import date, time, datetime
from pydantic import BaseModel
from . import models
...
class TimeSheetRange(BaseModel):
user_id: int
start: date
total: time
class Config:
orm_mode = True
crud.py
def get_timesheet_by_date_range_and_user(db: Session, user_id=int, date_start=datetime, date_end=datetime):
return db.query(models.TimeSheet.user_id, \
func.date(models.TimeSheet.start).label('start'), \
func.sec_to_time(func.sum(func.timediff(models.TimeSheet.end,models.TimeSheet.start))).label('total') ) \
.filter(models.TimeSheet.user_id == user_id) \
.filter(func.date(models.TimeSheet.start) >= date_start) \
.filter(func.date(models.TimeSheet.end) <= date_end) \
.group_by(func.date(models.TimeSheet.start)) \
.all()
main.py
@app.get("/timesheets/range/", response_model=List[schemas.TimeSheetRange])
def read_timesheets(user_id: int, date_start:date = datetime.now().date(), date_end:date = datetime.now().date(), db: Session = Depends(get_db)):
timesheets = crud.get_timesheet_by_date_range_and_user(db, user_id=user_id, date_start=date_start, date_end=date_end)
return timesheets
And the SQL Query that is build with a result:
SELECT
timesheet.user_id AS timesheet_user_id,
date(timesheet.start) AS start,
sec_to_time(sum(timediff(timesheet.end, timesheet.start))) AS total
FROM
timesheet
WHERE
timesheet.user_id = 7
AND
date(timesheet.start) >= '2020-09-20'
AND
date(timesheet.end) <= '2020-09-22'
GROUP BY
date(timesheet.start);
+-------------------+------------+----------+
| timesheet_user_id | start | total |
+-------------------+------------+----------+
| 7 | 2020-09-20 | 17:50:47 |
| 7 | 2020-09-21 | 18:21:11 |
+-------------------+------------+----------+
Is there something wrong with the datatype time
or am i missing something really stupid?
Upvotes: 2
Views: 4773
Reputation: 334
I think the issue here is that the Pydantic model you are using is expecting a time
object type, when it should be expecting a timedelta
type as your sql statement seems to indicate that you are calculating a time difference for the total
column.
So you Pydantic schemas.py file should read:
from typing import List, Optional
from datetime import date, datetime, timedelta
from pydantic import BaseModel
from . import models
...
class TimeSheetRange(BaseModel):
user_id: int
start: date
total: timedelta
class Config:
orm_mode = True
Upvotes: 1