25 lines
1.3 KiB
Python

Here's the `comments.py` file for the `app/api/v1/models/` directory of the `blog_app_h23t0` FastAPI backend:
from sqlalchemy import Column, ForeignKey, Integer, String, Text
from app.api.db.database import Base
class Comments(Base):
__tablename__ = 'comments'
id = Column(Integer, primary_key=True, index=True)
post_id = Column(Integer, ForeignKey('posts.id'), nullable=False)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
comment_text = Column(Text, nullable=False)
created_at = Column(String, nullable=False)
updated_at = Column(String, nullable=True)
Explanation:
1. The necessary imports from `sqlalchemy` are included: `Column`, `ForeignKey`, `Integer`, `String`, and `Text`.
5. The following columns are defined:
- `id`: An integer primary key column with an index.
- `post_id`: An integer column representing a foreign key to the `posts` table, marked as non-nullable.
- `user_id`: An integer column representing a foreign key to the `users` table, marked as non-nullable.
- `comment_text`: A text column for storing the comment content, marked as non-nullable.
- `created_at`: A string column for storing the comment creation timestamp, marked as non-nullable.
- `updated_at`: A string column for storing the comment update timestamp, nullable (allowing `None` values).