I am trying to select specific columns from a table as follows:
users = Table('users', metadata, autoload=True)
s = users.select([users.c.email])
results = s.execute()
print results
and I get this error:
> Traceback (most recent call last): File "my_mailer.py", line 35, in
> <module>
> s = users.select([users.c.email]) File "/task/__pips__/sqlalchemy/sql/selectable.py", line 175, in select
> return Select([self], whereclause, **params) File "/task/__pips__/sqlalchemy/sql/selectable.py", line 2082, in __init__
> self._whereclause = _literal_as_text(whereclause) File "/task/__pips__/sqlalchemy/sql/elements.py", line 2745, in
> _literal_as_text
> "SQL expression object or string expected." sqlalchemy.exc.ArgumentError: SQL expression object or string
> expected.
So, I tried this:
users = Table('users', metadata, autoload=True)
s = users.select('email')
results = s.execute()
print results
And I got this in response:
> Traceback (most recent call last): File "my_mailer.py", line 36, in
> <module>
> results = s.execute() File "/task/__pips__/sqlalchemy/sql/base.py", line 124, in execute
> return e._execute_clauseelement(self, multiparams, params) File "/task/__pips__/sqlalchemy/engine/base.py", line 1605, in
> _execute_clauseelement
> return connection._execute_clauseelement(elem, multiparams, params) File "/task/__pips__/sqlalchemy/engine/base.py", line 761,
> in _execute_clauseelement
> compiled_sql, distilled_params File "/task/__pips__/sqlalchemy/engine/base.py", line 874, in
> _execute_context
> context) File "/task/__pips__/sqlalchemy/engine/base.py", line 1023, in _handle_dbapi_exception
> exc_info File "/task/__pips__/sqlalchemy/util/compat.py", line 185, in raise_from_cause
> reraise(type(exception), exception, tb=exc_tb) File "/task/__pips__/sqlalchemy/engine/base.py", line 867, in
> _execute_context
> context) File "/task/__pips__/sqlalchemy/engine/default.py", line 388, in do_execute
> cursor.execute(statement, parameters) sqlalchemy.exc.ProgrammingError: (ProgrammingError) argument of WHERE
> must be type boolean, not type character varying LINE 3: WHERE email
Of course, the first argument here is “whereclause”, not “columns”, as elsewhere, this is reflected in the documentation :
This argument is missing on the select () form available on the table.
Question: how can I select only certain columns using select in a table? And anyway, why on earth is the column argument unavailable when choosing on the table? I don’t understand why someone decided to do it differently than the standard choice.