查询(模型)sqlalchemy与outerjoin有隐含的区别?

时间:2017-07-05 14:19:30

标签: python sqlalchemy flask-sqlalchemy

考虑以下模型:

class Order(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    status = db.Column(db.Enum('Ordered', 'Delivered'), default='Ordered', nullable=False)
    skus = db.relationship('SKU', order_by='SKU.index', back_populates='order', passive_deletes=True)

class SKU(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    order_id = db.Column(db.Integer, db.ForeignKey('order.id', onupdate='CASCADE', ondelete='CASCADE'), nullable=False)
    order = db.relationship('Order', lazy='joined', back_populates='skus', innerjoin=True)

    index = db.Column(db.Integer)

    supplier = db.Column(db.Integer)

db对象是来自SQLAlchemy的{​​{1}}的实例

测试数据是单个订单中的三个SKU:

flask_sqlalchemy

可以预期,使用显式外连接+----+---------+ | id | status | +----+---------+ | 10 | Ordered | +----+---------+ +----+----------+-------+----------+ | id | order_id | index | supplier | +----+----------+-------+----------+ | 28 | 10 | 0 | 10 | | 29 | 10 | 1 | 10 | | 30 | 10 | 2 | 10 | +----+----------+-------+----------+ Order会生成笛卡尔积并返回 3 行,但这不是真的。这将打印单个“Ordered”:

SKU

即使生成了正确的查询(并按预期返回 3 行):

orders = Order.query.outerjoin(SKU).filter(SKU.supplier == 10).all()
for order in orders:
    print order.status

我猜测SQLAlchemy会以某种方式检测重复的Order对象并将其从最终列表中删除。但这有记录吗?这背后的原因是什么?

同时,查询状态字段没有此问题: 3 行打印在此处:

SELECT `order`.id AS order_id, `order`.status AS order_status 
FROM `order` LEFT OUTER JOIN `SKU` ON `order`.id = `SKU`.order_id 
WHERE `SKU`.supplier = %(supplier_1)s

0 个答案:

没有答案