ITADN

paginate with join failed

#1423Closedhxzhao527 创建于 2026-04-18
H
hxzhao527commented
<!-- This issue tracker is a tool to address bugs in Flask-SQLAlchemy itself. Please use GitHub Discussions or the Pallets Discord for questions about your own code. Ensure your issue is with Flask-SQLAlchemy and not SQLAlchemy itself. Replace this comment with a clear outline of what the bug is. --> <!-- Describe how to replicate the bug. Include a minimal reproducible example that demonstrates the bug. Include the full traceback if there was an exception. --> <!-- Describe the expected behavior that should have happened but didn't. --> # Environment: - Python version: >=3.14 - Flask-SQLAlchemy version: 3.1.1 - SQLAlchemy version: 2.0.49 # how to reproduce ```python from flask import Flask, jsonify from flask_sqlalchemy import SQLAlchemy from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column from sqlalchemy import select as sql_select from dataclasses import dataclass class Base(DeclarativeBase): pass class User(Base): __tablename__ = "user" id: Mapped[int] = mapped_column(primary_key=True) username: Mapped[str] = mapped_column(unique=True) email: Mapped[str] class Other(Base): __tablename__ = "other" id: Mapped[int] = mapped_column(primary_key=True) username: Mapped[str] = mapped_column(unique=True) age: Mapped[int] db = SQLAlchemy(model_class=Base) app = Flask(__name__) # configure the SQLite database, relative to the app instance folder app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///project.db" # initialize the app with the extension db.init_app(app) with app.app_context(): db.drop_all() db.create_all() db.session.add(User(username="test", email="test@test.com")) db.session.add(Other(username="test", age=2026)) db.session.commit() @dataclass class UserInfo: username: str email: str age: int @app.route("/users") def user_list(): stmt = sql_select(User, Other).join(Other, User.username==Other.username).order_by(User.id) paginate = db.paginate(stmt) users = [] for user, other in paginate.items: users.append(UserInfo(username=user.username, email=user.email, age=other.age)) result = { "has_next": paginate.has_next, "users": users } return jsonify(result) if __name__ == "__main__": app.run(debug=True) ``` got unexpected error <img width="459" height="168" alt="Image" src="https://github.com/user-attachments/assets/91154bd6-d61b-4e26-b7c9-8c9ec3e85ec5" /> # why failed https://github.com/pallets-eco/flask-sqlalchemy/blob/168cb4b7b50fe5176307a10d873781bfafc6eeda/src/flask_sqlalchemy/pagination.py#L335-L339 `.scalars()` here causes. # how to fix ```python def _query_items(self) -> list[t.Any]: select = self._query_args["select"] select = select.limit(self.per_page).offset(self._query_offset) session = self._query_args["session"] return list(session.execute(select).unique().all()) ```
关闭于 2026-04-18 0 条评论