0

I am trying to write an AWS Lambda function in python that retrieves records from a table. I have no trouble running the code locally. However, when I run the code in AWS Lambda function, I get the error "not all arguments converted during string formatting".

The query is executed by the following code:

class SalesReceipt(declarative_base(), PersistentBase.PersistentBase): tablename = 'sales_receipts'

id = Column(BigInteger, primary_key=True, autoincrement=True)
externalId = Column(String(35), nullable=False)
accountId = Column(BigInteger, nullable=False)
customerId = Column(BigInteger, nullable=False)
orderNumber = Column(String(30), nullable=True)
orderType = Column(String(35), nullable=True)
saleDate = Column(DateTime, nullable=False)
saleAmount = Column(BigInteger, nullable=False)
taxAmount = Column(BigInteger, nullable=False)
totalAmount = Column(BigInteger, nullable=False)


def get_by_account_id(self, account_id):
    logger = logging.getLogger()
    message = '=== [SalesReceipt]account_id = %d' % (account_id)
    logger.info(message)
    data_service = ds.DataService()
    try:
        session = data_service.get_session()
        query = session.query(SalesReceipt).filter(SalesReceipt.accountId == account_id)
        #query = session.query(SalesReceipt)
        logger.info('=== [SalesReceipt]Query created = %s' % (str(query)))
        result = query.all()
        logger.info('=== [SalesReceipt]Query has been successfully executed.')
        return result
    except NoResultFound:
        logging.error('=== [SalesReceipt]No results found')
        return None
    except Exception as ex:
        logging.error('=== [SalesReceipt]Failed to retrieve sales receipts for the account.', ex)
        raise
    except:
        logging.error('=== [SalesReceipt]Caught un-handled exception.')
        raise

The error is encountered during statement result = query.all(). Nothing is executed after that and none of my catch blocks are entered.

I have googled this statement but have not found any clue as to why this is happening inside sqlalchemy functions. I have verified that the query is correct by printing the query out and executing it against the database manually.

I am also a newbie to python so any help will be appreciated.

1 Answer 1

2

Most likely the issue is in the following line -

logging.error('=== [SalesReceipt]Failed to retrieve sales receipts for the account.', ex)

ex is passed but never included in the string. Try the following instead -

logging.error('=== [SalesReceipt]Failed to retrieve sales receipts for the account - %s' % ex)

or

logging.error('=== [SalesReceipt]Failed to retrieve sales receipts for the account.')
Sign up to request clarification or add additional context in comments.

1 Comment

That was it. By fixing that error, I was able to get the real exception. The real exception turned out to be some typos so I was able to fix it easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.