0

I want to log a complete row from a sqlalchemy query (ORM), when a specific bug appears (in my example this is when multiple rows where found, but this has nothing to do with the question). At the moment i adress every column like this.

try:          
    result = query.one_or_none()
except MultipleResultsFound:
    self.logger.info('MultipleResultsFound!!')
    for row in query.all():
        self.logger.info('column1:{}, column2:{}, column3:{}'.
                          format(row.column1, row.column2, row.column3))    

But there must be a better way without adressing every column to show every column in the log. How can i display all columns from a row with one simple command ?

SuperShoot
  • 9,880
  • 2
  • 38
  • 55
Egirus Ornila
  • 1,234
  • 4
  • 14
  • 39
  • Have you try [this](https://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict)? – waynelpu May 01 '19 at 05:43
  • Yes but all these solutions do not work, i always get an attribute error if the query is like this: query(func.max(...)) – Egirus Ornila May 01 '19 at 06:39

1 Answers1

0

Try this method

Also note you can use .label to naming your func in query.

e.g. db.session.query(func.sum(SomeModel.something).label('total'))

for row in query.all():
  print(row._asdict())
SuperShoot
  • 9,880
  • 2
  • 38
  • 55
waynelpu
  • 425
  • 3
  • 9