Reputation: 271614
class Posts(Base):
__tablename__ = 'posts_posts'
id = Column(Integer, primary_key = True)
user_id = Column(Integer, nullable=False)
body = Column(Text, nullable=True)
created_at = Column(Date) << is this right?
updated_at = Column(Date) ??
I would also like the created_at
column to automatically set the date when it's created.
And also, the updated_at
column to set the date anytime this row is changed.
Upvotes: 16
Views: 18591
Reputation: 76952
You can provide default
and onupdate
parameters to Column
:
def _get_date():
return datetime.datetime.now()
class Posts(Base):
#...
created_at = Column(Date, default=_get_date)
updated_at = Column(Date, onupdate=_get_date)
See Column documentation for more info on this one.
Upvotes: 29