Adding CreatedAt Column

  • In the models.py file we should add the following:

from app.database import Base
from sqlalchemy import TEXT, TIMESTAMP, Column, Integer, String, Boolean, text
from sqlalchemy.sql.expression import null

class Post(Base):
    # Define Tablename
    __tablename__ = 'posts'

    # Define Columns
    id = Column(Integer, primary_key=True, nullable=False)
    title = Column(String, nullable=False)
    content = Column(String, nullable=False)
    published = Column(Boolean, server_default='TRUE', nullable=False)

    # This is how you create the Timestamp for the table
    created_at = Column(TIMESTAMP(timezone=True), nullable=False, server_default=text('now()'))

Last updated