首页 > 解决方案 > 为什么我收到索引错误异常错误?

问题描述

    try:

        # recheck this bank detail lookup, can optimize this

        # Known fact: as getting bank details and making refund is not in sync, we have bank_details against
        # one of the shipment id, so not sure of refund will go against specific bank details.
        # Current policy: taking latest one
        user_obj = self.session.query(User).filter_by(id=self.order_obj.user_id).one()
        current_shipment_id = self.shipment_id
        current_shipment_obj = self.session.query(Shipment).filter_by(id=current_shipment_id).one()
        prev_shipment_id = current_shipment_obj.previous_shipment_id
        bank_account_details = self.session.query(BankAccount).filter(
            BankAccount.affiliate_order_id==self.order_obj.affiliate_order_id,
            BankAccount.user_id==user_obj.mongo_user_id,
            BankAccount.shipment_id.in_([current_shipment_id, prev_shipment_id])
        ).order_by(desc('created_ts')).all()[0]  # IndexError

    except IndexError:
        logger.exception("Unable to process refund, as no bank details found")

{bag_id:51156,current_awb_no:'null',current_shipment_id:'16266090343871907281J',forward_awb_no:'null',forward_shipment_id:'null',invoice_no:'null',jm_order_id:'16260961159105232A',payment_id 异常索引:5118 索引:超出范围

标签: python

解决方案


因为这个说法

self.session.query(BankAccount).filter(
        BankAccount.affiliate_order_id==self.order_obj.affiliate_order_id,
        BankAccount.user_id==user_obj.mongo_user_id,
        BankAccount.shipment_id.in_([current_shipment_id, prev_shipment_id])
    ).order_by(desc('created_ts')).all()

长度小于 1,并且不可能从空列表中获取第一个元素。


推荐阅读