32 lines
960 B
Python

Here's the `posts.py` file for the `app/api/v1/models/` directory of the `blog_app` FastAPI backend:
from typing import Optional
from sqlalchemy import Column, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship
from app.api.db.database import Base
class Posts(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True, index=True)
title = Column(String, nullable=False)
content = Column(Text, nullable=False)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
user = relationship('Users', back_populates='posts')
def __repr__(self):
return f"Posts(id={self.id}, title='{self.title}', content='{self.content[:20]}...', user_id={self.user_id})"
Explanation:
Note: This code assumes the existence of a `Users` model class in the same directory. If the `Users` model does not exist, you may need to remove or modify the `user` relationship definition accordingly.