jonra1993 / fastapi-alembic-sqlmodel-async

This is a project template which uses FastAPI, Pydantic 2.0, Alembic and async SQLModel as ORM. It shows a complete async CRUD using authentication and role base access control.
MIT License
879 stars 143 forks source link

Cannot display is_mutual. #22

Closed dongfengweixiao closed 1 year ago

dongfengweixiao commented 1 year ago

https://github.com/jonra1993/fastapi-alembic-sqlmodel-async/blob/bb8018cad75b0caf83ce248508f9d949d02bf643/fastapi-alembic-sqlmodel-async/app/api/v1/endpoints/user.py#L120

    query = (
        select(User,UserFollow.is_mutual) <- this line
        .join(UserFollow, User.id == UserFollow.user_id)
        .where(UserFollow.target_user_id == current_user.id)
    )

how to display is_mutual info in this query? I tried many methods, but none of them could be realized.

jonra1993 commented 1 year ago

Hello, @dongfengweixiao mmm looks weird I am going to check it in these days.

dongfengweixiao commented 1 year ago

Hello, @dongfengweixiao mmm looks weird I am going to check it in these days.

Thank you. Besides, good morning!

dongfengweixiao commented 1 year ago
# user_crud.py
...
    async def get_followers_paginated(
        self,
        *,
        user_id: UUID,
        params: Optional[Params] = Params(),
        db_session: Optional[AsyncSession] = None
    ) -> Page[IUserReadWithoutGroups]:
        db_session = db_session or db.session

        query = (
            select(User, UserFollow.is_mutual)
            .join(UserFollow, User.id == UserFollow.user_id)
            .where(UserFollow.target_user_id == user_id)
        )
        result = await paginate(db_session, query, params) <- error in this line
        return result
...
--- 2022-10-25 15:13:08,653 INFO sqlalchemy.engine.Engine 
SELECT "User".birthdate, "User".follower_count, "User".following_count, "User".first_name, "User".last_name, "User".email, "User".is_active, "User".is_superuser, "User".role_id, "User".phone, "User".state, "User".country, "User".address, "User".id, "User".updated_at, "User".created_at, "User".hashed_password, "User".image_id, "UserFollow".is_mutual 
FROM "User" JOIN "UserFollow" ON "User".id = "UserFollow".user_id 
WHERE "UserFollow".target_user_id = %s 
 LIMIT %s OFFSET %s

深度截图_plasmashell_20221025231958

It looks like bug of fastapi_pagination.

jonra1993 commented 1 year ago

Hello, @dongfengweixiao thanks for this insight I am going to check it also.

jonra1993 commented 1 year ago

I see @dongfengweixiao solved this bug extracting User columns as independent fields here https://github.com/jonra1993/fastapi-alembic-sqlmodel-async/pull/26