33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
Here's the `user.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 User(Base):
|
|
__tablename__ = 'user'
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
username = Column(String, unique=True, index=True, nullable=False)
|
|
email = Column(String, unique=True, index=True, nullable=False)
|
|
hashed_password = Column(String, nullable=False)
|
|
is_active = Column(Integer, default=1)
|
|
bio = Column(Text, nullable=True)
|
|
|
|
posts = relationship("Post", back_populates="author")
|
|
|
|
def __repr__(self):
|
|
return f"User(id={self.id}, username='{self.username}', email='{self.email}')"
|
|
|
|
Explanation:
|
|
|
|
5. Defined columns for the `User` model:
|
|
- `id`: Integer primary key with index
|
|
- `username`: String, unique, indexed, and non-nullable
|
|
- `email`: String, unique, indexed, and non-nullable
|
|
- `hashed_password`: String, non-nullable
|
|
- `is_active`: Integer with a default value of 1
|
|
- `bio`: Text, nullable
|
|
|
|
Note: This code assumes that the `Post` model is defined in a separate file and imported accordingly. Additionally, you may need to adjust the imports and paths based on your project structure and dependencies. |