Skip to content

strange async sqlalchemy error with fastapi-pagination... #324

Answered by uriyyo
wu-clan asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @wu-clan,

I think I know where the problem is.
Could you please try changing get_all_users implementation to:

# This code doesn't return data correctly
@user.get('', summary='get all users', response_model=Page[GetUserInfo], dependencies=[Depends(get_current_user)])
async def get_all_users(db: AsyncSession = Depends(get_db)) -> Any:
    users = select(User).order_by(User.time_joined.desc()).options(selectinload(User.roles).joinedload(Role.menus))
    return await paginate(db, users)

Basically, you need to also load Role.menus relationship because it's required by RoleAll schema.

P.S. Little advice from me - when you have one-to-many or many-to-many relationships it's better to load the…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by wu-clan
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants