Home > Enterprise >  SQLalchemy select using ORM the resulting row contains only memory location
SQLalchemy select using ORM the resulting row contains only memory location

Time:10-28

I think I have misunderstood something critical and obvious, I am attempting to use the SqlAlchemy ORM to select a row from a table in a database and return the values. The first step is to get the row and access it, from there I think I'm good however, when I select the row and try and investigate the object that is returned, all I find is an address for the object in memory: <main.UserMap object at 0x000001F65A54B490>,

Attempting to use objprint to investigate the object gives no further information, I am confused as per my understanding the resulting row object should behave like a tuple so at least objprint should find a variety of entries within it even if it can't print them due to them being differen't data types.

The statement in question: select(UserMap).where(UserMap.google_auth == '***********************')

a more basic select(UserMap) also seems to give a similar result.

The table contains some fields as strings, some as integers and some in date and time formats but obviously only one type per column.

I am using session.execute, I would like to avoid query as I understand that it's functionality is being deprecated in the 2.x API, if I have misunderstood this then I am happy to attempt that but would still like to understand what I am getting wrong about the result and row objects.

As I said, I think I have missed something important, but don't realise what, as far as I can see I am executing the statements in much the same way as shown in the ORM documentation e.g:

>>> stmt = select(User).where(User.name == 'spongebob')
>>> with Session(engine) as session:
...     for row in session.execute(stmt):
...         print(row)

CodePudding user response:

<main.UserMap object at 0x000001F65A54B490> is the default string representation of an ORM object. You can retrieve the attributes (properties) of the object using the standard syntax:

me = UserMap(user_name="Gord")
print(me)  # <main.UserMap object at 0x000001F65A54B490>
print(me.user_name)  # Gord
  • Related