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

This commit is contained in:
Backend IM Bot 2025-03-21 11:21:48 +01:00
parent b940724847
commit 8f6f9f00f3
6 changed files with 107 additions and 24 deletions

View File

@ -1,3 +1,6 @@
Here's a `comments.py` file for the `app/api/v1/models/` directory of the `blog_app_h23t0` 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
@ -6,11 +9,17 @@ class Comments(Base):
__tablename__ = 'comments'
id = Column(Integer, primary_key=True, index=True)
post_id = Column(Integer, ForeignKey('posts.id'))
user_id = Column(Integer, ForeignKey('users.id'))
comment_text = Column(Text)
created_at = Column(Integer)
updated_at = Column(Integer)
post_id = Column(Integer, ForeignKey('posts.id'), nullable=False)
author_id = Column(Integer, ForeignKey('users.id'), nullable=False)
content = Column(Text, nullable=False)
created_at = Column(Integer, nullable=False)
updated_at = Column(Integer, nullable=True)
post = relationship('Post', back_populates='comments')
user = relationship('User', back_populates='comments')
post = relationship('Posts', back_populates='comments')
author = relationship('Users', back_populates='comments')
def __repr__(self):
return f"Comments(id={self.id}, post_id={self.post_id}, author_id={self.author_id}, content='{self.content[:20]}...', created_at={self.created_at}, updated_at={self.updated_at})"
The class includes the following columns:

View File

@ -1,3 +1,7 @@
Sure, here's the `posts.py` file for the `blog_app_h23t0` 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
@ -6,8 +10,26 @@ class Posts(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True, index=True)
title = Column(String)
content = Column(Text)
user_id = Column(Integer, ForeignKey('users.id'))
title = Column(String, nullable=False)
content = Column(Text, nullable=False)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
user = relationship('User', back_populates='posts')
user = relationship('Users', back_populates='posts')
comments = relationship('Comments', back_populates='post')
def __repr__(self):
return f"Posts(id={self.id}, title='{self.title}', user_id={self.user_id})"
Explanation:
1. We import the necessary modules from SQLAlchemy: `Column`, `ForeignKey`, `Integer`, `String`, and `Text`.
6. We define the columns for the `Posts` model:
- `id`: Integer primary key with index
- `title`: String column, cannot be null
- `content`: Text column, cannot be null
- `user_id`: Integer foreign key referencing the `id` column of the `users` table, cannot be null
7. We define the relationships:
- `user`: One-to-many relationship with the `Users` model, backref `'posts'`
- `comments`: One-to-many relationship with the `Comments` model, backref `'post'`
Note: This code assumes that you have a `Users` model defined elsewhere in your project, and that you will define a `Comments` model as well. You may need to adjust the relationship definitions and foreign key constraints based on your project's requirements.

View File

@ -1,12 +1,32 @@
from sqlalchemy import Column, ForeignKey, Integer, String, Text
Sure, here's an example of the `tags.py` file for the `blog_app` FastAPI backend:
from typing import Optional
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from app.api.db.database import Base
class Tags(Base):
__tablename__ = 'tags'
__tablename__ = "tags"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False, unique=True)
description = Column(Text)
description = Column(String, nullable=True)
posts = relationship("Post", back_populates="tags", secondary="post_tags")
posts = relationship("PostTags", back_populates="tag")
def __repr__(self):
return f"Tag(id={self.id}, name='{self.name}', description='{self.description}')"
Explanation:
1. We import the necessary modules from SQLAlchemy (`Column`, `ForeignKey`, `Integer`, `String`) and `relationship` from `sqlalchemy.orm`.
4. We define the following columns for the `Tags` model:
- `id`: an integer primary key with an index.
- `name`: a string column that cannot be null and must be unique.
- `description`: an optional string column that can be null.
5. We define a relationship with the `PostTags` model (not shown here) using the `relationship` function from SQLAlchemy. This relationship is bi-directional, as indicated by `back_populates="tag"`.
Note: This example assumes that you have a `PostTags` model (or a similar model) defined elsewhere in your application to represent the many-to-many relationship between posts and tags.

View File

@ -1,3 +1,6 @@
Here's the `user.py` file for the `blog_app` FastAPI backend, located in the `app/api/v1/models/` directory:
from typing import Optional
from sqlalchemy import Column, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship
from app.api.db.database import Base
@ -6,9 +9,39 @@ class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True)
hashed_password = Column(String)
is_active = Column(Integer, default=1)
username = Column(String, unique=True, nullable=False)
email = Column(String, unique=True, nullable=False)
password = Column(String, nullable=False)
full_name = Column(String)
bio = Column(Text)
posts = relationship("Post", back_populates="author")
comments = relationship("Comment", back_populates="author")
def __repr__(self):
return f"User(id={self.id}, username='{self.username}', email='{self.email}')"
Explanation:
1. Imports:
- `typing.Optional`: Used for type hints.
- `sqlalchemy`: Imported `Column`, `ForeignKey`, `Integer`, `String`, and `Text` classes for defining table columns.
- `sqlalchemy.orm`: Imported `relationship` for defining relationships between models.
- `app.api.db.database`: Imported `Base` class, which is the base class for all SQLAlchemy models.
2. `User` class:
- Inherits from the `Base` class.
- `__tablename__` attribute specifies the name of the database table for this model.
- Columns:
- `id`: Integer primary key column with an index.
- `username`: String column, unique and not nullable.
- `email`: String column, unique and not nullable.
- `password`: String column, not nullable.
- `full_name`: String column.
- `bio`: Text column.
- Relationships:
- `posts`: One-to-many relationship with the `Post` model, where a user can have multiple posts.
- `comments`: One-to-many relationship with the `Comment` model, where a user can have multiple comments.
- `__repr__` method: Provides a string representation of the `User` object for debugging purposes.
Note: The `Post` and `Comment` models are not defined in this file, but they are assumed to exist and have the appropriate relationships defined with the `User` model.

View File

@ -2,7 +2,7 @@ from pydantic import BaseModel
class PostsBase(BaseModel):
title: str
content: str
body: str
class Posts(PostsBase):
class Config:

View File

@ -1,11 +1,10 @@
from pydantic import BaseModel
class UserBase(BaseModel):
email: str
pass
class User(UserBase):
class User(BaseModel):
id: int
is_active: bool
class Config:
orm_mode = True