I'm currently setting up a class using SQL Alchemy. This class has a start_date
attribute, defined such as:
from sqlalchemy import Column, DateTime
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Foo(Base):
__tablename__ = "foo"
id = Column(UUID(as_uuid=True), primary_key=True)
start_date = Column(DateTime, nullable=True)
Yet, I'm facing a type issue:
foo = Foo(start_date="2019-05-05")
print(foo.start_date, type(foo.start_date))
# 2019-05-05 <class 'str'>
I would expect a datetime here, especially as when fetching the same record from the database, I retrieve a date time object:
foo = Foo(id=uuid.uuid4(), start_date="2019-05-05")
db_session.add(foo)
db_session.commit()
db_session.refresh(foo)
print(foo.start_date, type(foo.start_date))
# 2019-03-01 00:00:00 <class 'datetime.datetime'>
Is there any way to force a cast from the Column
declaration?