I'm working on a FastAPI project that uses SQLModel as ORM. As far as I understand it SQLModel is some sort of wrapper on top of sqlalchemy and pydantic that makes the two work together. I have a model that looks like this:
from psycopg2.extras import DateTimeTZRange
from pydantic import conlist
from sqlalchemy import Column
from sqlalchemy.dialects.postgresql import TSTZRANGE
from sqlmodel import Field
from app.models.base import BaseModel
class MyModel(BaseModel, table=True):
# time_range: conlist(datetime, min_items=2, max_items=2) = Field(
time_range: DateTimeTZRange = Field(
sa_column=Column(TSTZRANGE()), nullable=False, index=False
)
class Config:
arbitrary_types_allowed = True
When I try to save an instance of this model, I get the following error:
sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.DataError'>: invalid input for query argument $1: DateTimeTZRange(datetime.datetime(2019, ... (list, tuple or Range object expected (got type <class 'psycopg2._range.DateTimeTZRange'>))
[SQL: INSERT INTO my_model (time_range) VALUES (%s) RETURNING my_model.id]
[parameters: (DateTimeTZRange(datetime.datetime(2019, 4, 2, 23, 0, tzinfo=<UTC>), datetime.datetime(2019, 4, 2, 23, 30, tzinfo=<UTC>), '[)'))]
The commented line (using conlist
) didn't give any errors, but it always saved null
to the database. I couldn't find any resources or documentation online regarding how to make this work. Any ideas what I might be doing wrong?