简体   繁体   中英

Convert sql statement into flask_sqlalchemy with pagination

I want to convert this statement into Flask-Sqlalchemy equivalent with pagination.


SELECT * FROM file JOIN course_files ON file.id == course_files.file_id JOIN course_students ON course_students.course_id = course_files.course_id and course_students.user_id = 1

Where the user_id could vary.

I have tried below query but it doesn't filter by the user id for unknown reasons


db.session.query(File).join(course_files).join(
        course_students, (
                course_students.c.course_id == course_files.c.course_id and
                course_students.c.user_id == 1
        )).all()

Okay found the answer.


db.session.query(File
).join(
         course_files, File.id == course_files.c.file_id
).join(
         course_students, course_students.c.course_id == course_files.c.course_id
).filter(
         course_students.c.user_id == current_user.id
).paginate(current_page, per_page, errors_out=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM