Using Flask/sqlalchemy - say I have multiple tables related to each other. Classic example: orders - Lineitems - products. How would I go about properly query the tables if I wanted all orders with all line items and all products. Here is my attempt:
query = Orders.query
.join(Lineitems, Orders.id==Lineitems.orders_id)
.join(Products, Lineitems.product_id == Product.id)
.all()
Models
class Orders(db.Model):
id = db.Column(db.Integer, primary_key=True)
lineitems = db.relationship("Lineitems", backref="orders")
class Lineitems(db.Model):
id = db.Column(db.Integer, primary_key=True)
order_id = db.Column(db.Integer, db.ForeignKey('orders.id'))
product_id= db.Column(db.Integer, db.ForeignKey('products.id'))
products = db.relationship("Products", backref='products', foreign_keys=[product_id])
class Products(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)