28 lines
1.3 KiB
Python
28 lines
1.3 KiB
Python
Here's the `comments.py` file for the `app/api/v1/models/` directory of the `blog_app_svkgt` FastAPI backend, defining a SQLAlchemy model for comments:
|
|
|
|
from sqlalchemy import Column, ForeignKey, Integer, String, Text
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from app.db 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})"
|
|
|
|
This code defines a `Comment` model that inherits from the `Base` class provided by SQLAlchemy. The `Comment` model has the following fields:
|
|
|
|
|
|
|
|
|
|
This model assumes that there is a `Post` model defined elsewhere in the project, which has a corresponding one-to-many relationship with the `Comment` model (i.e., one post can have multiple comments).
|
|
|
|
Make sure to import the necessary modules (`sqlalchemy` and `app.db`) at the beginning of the file, and update the import paths if necessary to match your project structure. |