I have three tables in my database:
Links
-----------------------
id | student| class
1 | 2 | 1
2 | 1 | 1
3 | 1 | 2
Student
-----------------------
id | name | age
1 | Bob | 16
2 | Jane | 17
Class
_______________________
id | subject
1 | Math
2 | Science
I want to be able to look at the links table to see what students are in what class and return the results from the student table per class. So, if I were looking at a page that had class information I would get all the students information that are in that class. I want to be able to use Sqlalchemy to make this query, which is what's confusing me since I can't just a straight up JOIN or something.
I'll be passing the class information to the function in Flask, like:
@main.route('/class/<class_id>')
@login_required
def view_class(class_id):
Any help would be appreciated!
You can try this below and since class keyword is reserved in python, you need to use a different column name for Links Table and i have used it below as student_class
@main.route('/class/<class_id>')
@login_required
def view_class(class_id):
output = session.query(Student).join(Link, Student.id == Link.student).join(Class, Link.student_class == Class.id).filter(Class.id == class_id).all()
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.