-1

How can I iterate each column of a resultproxy object?

I've tried an answer from this question:

for col in class_mapper(obj.__class__).mapped_table.c)

Gets this error:

sqlalchemy.orm.exc.UnmappedClassError: Class 'sqlalchemy.engine.result.RowProxy' is not mapped
Ilja Everilä
  • 40,618
  • 6
  • 82
  • 94
el_pup_le
  • 9,721
  • 18
  • 73
  • 126

1 Answers1

1

A RowProxy is not a mapped class, as the error is telling you. It represents a lower level of abstraction and models a database result row "directly" – no ORM involved. Reading the documentation, a RowProxy is partly akin to an ordered dictionary, and provides the mapping interface, so you can use keys() and items() to iterate over keys and key, item tuples respectively. If you iterate over a RowProxy instance, you get the values.

So if you want to iterate over the key, value tuples of a row:

for row in result:
    for key, value in row.items():
        pass
Ilja Everilä
  • 40,618
  • 6
  • 82
  • 94