24 lines
863 B
Python

Here's a `comments.py` file with a SQLAlchemy model for comments, following the FastAPI project structure with SQLite and SQLAlchemy:
from sqlalchemy import Column, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship
from app.db.base_class import Base
class Comment(Base):
__tablename__ = "comments"
id = Column(Integer, primary_key=True, index=True)
content = Column(Text, nullable=False)
author = Column(String, nullable=False)
post_id = Column(Integer, ForeignKey("posts.id"), nullable=False)
post = relationship("Post", back_populates="comments")
def __repr__(self):
return f"Comment(id={self.id}, content='{self.content[:20]}...', author='{self.author}', post_id={self.post_id})"
Explanation:
5. The `content` column stores the text content of the comment and is required (`nullable=False`).