25 lines
868 B
Python

Here's the `comments.py` file with a SQLAlchemy model for comments:
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(100), 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}, author='{self.author}', content='{self.content[:20]}...')"
Explanation:
Note: This code assumes that you have already defined the `Post` model in a separate file, and that the necessary imports and database setup are handled elsewhere in your FastAPI project.