簡體   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