from sqlalchemy import Column, String, Integer, DateTime, ForeignKey, func from sqlalchemy.orm import relationship from database import Base class Post(Base): __tablename__ = "posts" id = Column(String, primary_key=True, index=True) username = Column(String, nullable=False) content = Column(String, nullable=False) created_at = Column(DateTime(timezone=True), server_default=func.now()) updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) comments = relationship("Comment", back_populates="post", cascade="all, delete-orphan") likes = relationship("Like", back_populates="post", cascade="all, delete-orphan") class Comment(Base): __tablename__ = "comments" id = Column(String, primary_key=True, index=True) post_id = Column(String, ForeignKey("posts.id", ondelete="CASCADE"), nullable=False) username = Column(String, nullable=False) content = Column(String, nullable=False) created_at = Column(DateTime(timezone=True), server_default=func.now()) updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) post = relationship("Post", back_populates="comments") class Like(Base): __tablename__ = "likes" post_id = Column(String, ForeignKey("posts.id", ondelete="CASCADE"), primary_key=True) username = Column(String, primary_key=True) created_at = Column(DateTime(timezone=True), server_default=func.now()) post = relationship("Post", back_populates="likes")