SQLAlchemy.all()只返回多重联接的第一个结果

2024-04-28 03:33:56 发布

您现在位置:Python中文网/ 问答频道 /正文

你好

因此,我使用以下查询连接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

如您所见,它仅从trip pallet table获取第一个托盘id,即1052。它还应该得到托盘标识1054,并返回该表上的所有包裹。你知道吗

有没有人知道如何让它返回第二个托盘从旅行托盘表和它的所有相应的包裹标识?你知道吗


Tags: andtheidpackageorderfollowinghasjoin