繁体   English   中英

SQLAlchemy .all()仅在多联接上返回第一个结果

[英]SQLAlchemy .all() only returning first result on multi-join

你好,

因此,我使用以下查询来连接5个表:

order = db.session.query(Trailer, Trip, TripPallet, WaveOrder, Package)\
    .join(Trip, Trailer.trailer_id == Trip.trailer_id)\
    .join(TripPallet, Trip.trip_id == TripPallet.trip_id)\
    .join(WaveOrder, TripPallet.pallet_id == WaveOrder.pallet_id)\
    .join(Package, WaveOrder.order_id == Package.order_id)\
    .filter(Trailer.trailer_id == '555555').all()

现在,这5个表如下:

**Trailer Table**
trailer_id      
555555

**Trip Table**
trip_id         trailer_id      
523462462       555555

**Trip Pallet table**
trip_id         pallet_id       
523462462       1052
523462462       1054

**Wave Order Table**
pallet_id       order_id
1052            123456
1052            123457
1054            324567
1054            797453

**Package table**
order_id        Tracking
123456            ABCKDF
123457            DVNIUO
324567            DNKLIN
797453            ADFNLN

因此,为了让您有个概览,拖车包含旅行。 该行程包含货盘,每个货盘包含波浪,这些波浪是一组包装。

我在上面使用的查询将返回一个托盘上的所有包装,但不会返回所有托盘。

当我通过for循环运行查询时,这是我的结果:

counter = 0
for trailer, trip, trippallet, wave_order, package in order:
    counter += 1
    print "%d The trailer id is %d and it contains the trip id %d with the pallet id %d and has the following order %d which " \
          "has the following tracking number %s" % \
          (counter, trailer.trailer_id, trip.trip_id, trippallet.pallet_id, wave_order.order_id, package.tracking_no)  

1 The trailer id is 555555 and it contains the trip id 523462462 with the pallet id 1052 and has the following order 123456 which has the following tracking number ABCKDF
2 The trailer id is 555555 and it contains the trip id 523462462 with the pallet id 1052 and has the following order 123457 which has the following tracking number DVNIUO

如您所见,它仅从行程托盘表中获取第一个pallet_id,即1052。它还应该获取pallet_id 1054,并返回该表上的所有包裹。

有谁知道如何使其从Trip Pallet表中返回第二个Pallet_id及其所有对应的包装?

你好,

问题不在于join语句。 join语句是100%正确的。 实际上,它是数据库。

DBA删除了我完成join语句所需的关键数据。

暂无
暂无

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

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