Skip to content

Using sqlalchemy paginate with multiple entities at once #650

Closed Answered by uriyyo
gfguilherme asked this question in Q&A
Discussion options

You must be logged in to vote

@gfguilherme Could you please try code bellow:

class AddressSchema(BaseModel):
    state: str

    class Config:
        orm_mode = True


class UserSchema(BaseModel):
    id: int
    name: str
    email: str

    class Config:
        orm_mode = True


class SearchOutSchema(BaseModel):
    user: UserSchema
    address: AddressSchema

    class Config:
        orm_mode = True


@app.get(
    "/users",
    response_model=Page[SearchOutSchema],
)
def get_users(db: Session = Depends(get_db)) -> Any:
    return paginate(
        db,
        select(User, Address.state).join(Address, User.id == Address.user_id).order_by(User.id, Address.id),
        transformer=lambda items: [{"user": user, "ad…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@gfguilherme
Comment options

@uriyyo
Comment options

Answer selected by gfguilherme
@uriyyo
Comment options

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