Update generated backend for blog_app with entities: posts, comments, tags, user

This commit is contained in:
Backend IM Bot 2025-03-21 10:33:38 +01:00
parent 9fa6b8df88
commit ba552aff74
4 changed files with 48 additions and 3 deletions

View File

@ -1,8 +1,11 @@
Here's the `dependencies.py` file for the `app/api/core/dependencies/` directory:
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.api.db.database import SessionLocal from app.api.db.database import SessionLocal
def get_db(): def get_db():
db = SessionLocal() db = SessionLocal()
try: try:
yield db yield db
finally: finally:
db.close() db.close()

View File

@ -1,3 +1,5 @@
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 sqlalchemy import Column, ForeignKey, Integer, String, Text
from app.api.db.database import Base from app.api.db.database import Base
@ -5,3 +7,19 @@ class Comments(Base):
__tablename__ = 'comments' __tablename__ = 'comments'
id = Column(Integer, primary_key=True, index=True) 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).

View File

@ -1,7 +1,27 @@
Here's the `user.py` file for the `app/api/v1/models/` directory in the `blog_app` FastAPI backend:
from sqlalchemy import Column, ForeignKey, Integer, String, Text from sqlalchemy import Column, ForeignKey, Integer, String, Text
from app.api.db.database import Base from app.api.db.database import Base
class User(Base): class User(Base):
__tablename__ = 'user' __tablename__ = "user"
id = Column(Integer, primary_key=True, index=True) id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, nullable=False)
email = Column(String, unique=True, nullable=False)
password_hash = Column(String, nullable=False)
full_name = Column(String)
bio = Column(Text)
profile_pic = Column(String)
Explanation:
1. We import the necessary classes from `sqlalchemy` for defining the database columns: `Column`, `ForeignKey`, `Integer`, `String`, and `Text`.
5. We define the following columns for the `User` model:
- `id`: An integer primary key column with an index.
- `username`: A string column that must be unique and cannot be null.
- `email`: A string column that must be unique and cannot be null.
- `password_hash`: A string column that cannot be null (for storing the hashed password).
- `full_name`: An optional string column for the user's full name.
- `bio`: An optional text column for the user's biography.
- `profile_pic`: An optional string column for the user's profile picture URL.

View File

@ -1 +1,5 @@
# No code generated fastapi
uvicorn
sqlalchemy
pydantic
loguru