0

Below code works fine

table = Table('user_data',metadata,autoload=True,autoload_with=conn)
stmt = select([table.columns.user_id,table.columns.username])
results = conn.execute(stmt).fetchall()
print(results)

But when i try to pass the columns as list (from postresql db) it fails

cols = get_table_cols() -- it returns a list -> [table.columns.user_id,table.columns.username]
stmt = select(cols )
results = conn.execute(stmt).fetchall()
print(results)

Error:

 sqlalchemy.exc.ArgumentError: Textual column expression 'table.c.username' should be explicitly declared with text('table.c.username'), or use column('table.c.username') for more specificity

Kindly provide inputs on the same

2
  • Can you provide the error logs? Commented Nov 17, 2021 at 10:41
  • added error details. tried with column () and text() but no luck Commented Nov 17, 2021 at 13:35

1 Answer 1

1

I've just recently experienced something similar to this. Keep in mind my engine had the 'Future' attribute set to True (in case this doesn't work for you)

I can't remember why simply unpacking the list like this didn't work (which it might in your case)

stmt = select(*cols)

But this ended up being my solution

cols = get_table_cols()
#assuming cols does return a list
stmt = select(*[c for c in cols])

#alternatively you can get the columns directly from the table object
#stmt = select(*[c for c in table.c])
results = conn.execute(stmt).fetchall()

#session.execute(stmt).all() -> what I did for ORM

print(results)

If you are using declarative classes for ORM, the table object can instead be accessed with

ClassName.__table__
Sign up to request clarification or add additional context in comments.

Comments

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.