简体   繁体   English

如何使用 Flask SQLAlchemy 中第 4 个表中的键查询 4 ​​个表?

[英]How to Query 4 tables with the key in the 4th table in Flask SQLAlchemy?

I have four tables with the structure as below:我有四个表,其结构如下:

class Vendors(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    companyName = db.Column(db.String(100))
    billingAddress = db.Column(db.String(100))
    shippingAddress = db.Column(db.String(100))
    contactPersonName = db.Column(db.String(100))
    contactPersonNumber = db.Column(db.String(100))
    gstNo = db.Column(db.String(100))
    openingBalance = db.Column(db.BIGINT)
    creditPeriod = db.Column(db.String(100))
    vendorCode = db.Column(db.String(100))
    vendorProducts = db.relationship('VendorsProduct', backref="vendProd")

class VendorsProduct(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    bf = db.Column(db.Integer)
    gsm = db.Column(db.Integer)
    types = db.Column(db.String(100))
    itemCode = db.Column(db.String(100))
    vendorID = db.Column(db.String(100), db.ForeignKey('vendors.vendorCode'))
    vendorInwards = db.relationship('VendorsInward', backref="vendInward")


class VendorsInward(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    reel = db.Column(db.String(100))
    deckle = db.Column(db.Integer)
    weight = db.Column(db.Integer)
    vendorInwardID = db.Column(
        db.String(100), db.ForeignKey('vendors_product.itemCode'))
    reelID = db.relationship('ReelStatusTable', backref="reelStatusTable")


class ReelStatusTable(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    reelStatus = db.Column(db.String(100))
    reelCode = db.Column(db.String(100), db.ForeignKey('vendors_inward.reel'))

In the ReelStatusTable under the reelStatus I have values named In Stock .ReelStatusTable下的reelStatus我有名为In Stock值。 I want to query in Flask-SQLAlchemy all the In Stock entries associated with a Vendors table.我想在 Flask-SQLAlchemy 中查询与Vendors表关联的所有In Stock条目。

I tried the following code and it simply displays all the values from the reelStatus and I cannot only query the In Stock ones :(我尝试了以下代码,它只是显示了reelStatus所有值,而我不能只查询In Stock的值:(

vendorsProductsList = db.session.query(
        Vendors
    ).join(
        VendorsProduct
    ).join(
        VendorsInward
    ).join(
        ReelStatusTable
    ).filter(
        ReelStatusTable.reelStatus == "In Stock"
    ).all()

Please help me query the In Stock entries.请帮我查询In Stock条目。

NOTE: I want to use Vendors.name , VendorsProduct.bf , VendorsProduct.gsm , VendorsProduct.types , VendorsInward.reel, VendorsInward.deckle, VendorsInward.weight and ReelStatusTable.reelStatus fields in my flask application注意:我想在我的烧瓶应用程序中使用Vendors.nameVendorsProduct.bfVendorsProduct.gsmVendorsProduct.typesVendorsInward.reel, VendorsInward.deckle, VendorsInward.weightReelStatusTable.reelStatus字段

Thank you in Advance.先感谢您。

It worked after I tried this query !!在我尝试这个查询后它起作用了!!

vendorsProductsList = db.session.query(Vendors,
                                           VendorsProduct,
                                           VendorsInward,
                                           ReelStatusTable
                                           ).filter(
                                               ReelStatusTable.reelStatus == "In Stock"
                                               ).filter(
                                                   ReelStatusTable.reelCode == VendorsInward.reel
                                                   ).filter(
                                                       VendorsInward.vendorInwardID == VendorsProduct.itemCode
                                                       ).filter(
                                                           VendorsProduct.vendorID == Vendors.vendorCode
                                                           ).all()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 3 个表的组合插入具有 3 个对应列的第 4 个表中? - How to insert combinations from 3 tables into a 4th table with 3 corresponding columns? SQL 连接 3 个表,然后在单个查询中从第 4 个表中提取数据 - SQL joins 3 tables and then pull data from 4th table in single query 如何对关联表进行FLASK SQLALCHEMY查询 - How to do FLASK SQLALCHEMY query for Association Table 使用关系表JOIN查询Flask SQLAlchemy相关表 - JOIN Query Flask SQLAlchemy Related Tables using relationships table Flask Sqlalchemy:用外键查询数据库 - Flask Sqlalchemy: Query a database with a foreign key PHP加入3个表,然后插入到第4 - php join 3 tables then insert to 4th 我在MySQL中有4个表-与没有重复的第4个表联接(可能为null值) - I have 4 tables in MySQL - Join with 4th table without duplicates (possible null value) SQL根据某些条件将数据从3个表合并到第4个表 - SQL Combining data from 3 tables to from a 4th table based on some conditions 与Mysql和Propel的额外字段和第四表外键的多对多关系 - Many to Many Relationship with extra field and 4th table foreign key with Mysql and Propel 如何在第 4 列包含与另一个表相同的 ID 的表中显示 3 列中的所有数据? - How to display all data in 3 columns from a table where the 4th column contains the same ID as another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM