I'm trying to create a Pydantic model with Tortoise ORM with Postgresql as the backend. I have the columns start_time
and end_time
defined in Postgres as a Time datatype. I tried my class as below:
class Carb_Ratio(models.Model):
id = fields.UUIDField(pk=True, auto_generated=True)
client_id = fields.UUIDField()
ratio_type = fields.CharEnumField(RatioType)
carb_ratio = fields.DecimalField(max_digits=10, decimal_places=2)
start_time = fields.CharField(max_length=8)
end_time = fields.CharField(max_length=8)
insulin_sensitivity = fields.DecimalField(max_digits=10, decimal_places=2)
created_at = fields.DatetimeField(auto_now_add=True)
When i try to create a record i end up with an error message "tortoise.exceptions.OperationalError: invalid input for query argument $5: '' ('str' object has no attribute 'hour')"
Tortoise fields do not have a TimeField, I tried even with fields.DatetimeField gives me an invalid datetime format for "08:00:00"
Any pointers in the right direction would be of help. Thanks in advance
Update: @Adrian Klaver thank you for your suggestion. I've changed these fields to DatetimeField
start_time = fields.DatetimeField()
end_time = fields.DatetimeField()
my input values has to be changed from
{
"client_id": "9181233d-0fa3-4844-bb28-ba1660be1415",
"ratio_type": "time_range",
"carb_ratio": 16,
"start_time": "17:00:01",
"end_time": "21:00:00",
"insulin_sensitivity": 6
}
to
{
"client_id": "9181233d-0fa3-4844-bb28-ba1660be1415",
"ratio_type": "time_range",
"carb_ratio": 16,
"start_time": "1900-01-01 17:00:01",
"end_time": "1900-01-01 21:00:00",
"insulin_sensitivity": 6
}
and now it is able to insert values properly. However the select function now errors with "iso8601.iso8601.ParseError: Expecting a string datetime.time(10, 0)"
$5
. Best guess is you are assuming you are working with adatetime.time
object when you are in fact you are working with astr
object.time
type then your changes to the model type and the input values will not work."1900-01-01 17:00:01"
is not a valid time format.