python - SQLAlchemy .all() 只返回多连接的第一个结果

标签 python mysql sqlalchemy

你好,

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

因此,为了给您一个概览,预告片包含一次旅行。该行程包含托盘,每个托盘包含 wave,它们是一组包裹。

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

当我通过 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 表中获取第一个 pallet_id,即 1052。它还应该获取 pallet_id 1054 并返回该表中的所有包裹。

有谁知道如何让它返回 Trip Pallet 表中的第二个 pallet_id 及其所有对应的包裹?

最佳答案

你好,

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

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

关于python - SQLAlchemy .all() 只返回多连接的第一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31413102/

相关文章:

python - 计算列表中具有相同值的子列表

Python相互依赖类(循环依赖)

python - 在 Python 中使用 ReportLabs 生成报告

mysql - 如何在MySQL中创建也是主键的外键?

mysql - 如何扩展 docker mysql 镜像

mysql - 查询没有时间的 date_created 与没有时间的 last_updated 相同

python - 如何在flask中使用after_request关闭数据库连接和python?

python - sqlalchemy.exc.InvalidRequestError : One or more mappers failed to initialize - can't proceed with initialization of other mappers 错误

python - SQLALCHEMY or_ 列表理解

Python 条件异常消息