How to pass a table as a function parameter?

55 views
Skip to first unread message

Tim Radke

unread,
Aug 21, 2015, 11:44:59 PM8/21/15
to sqlalchemy
I'm using SQLAlchemy version 0.9.8, connecting to Postgres 9.4. I'm trying to do something like this:

query = select([func.to_json(User)]).select_from(User)

# Printing the query returns something that seems to make sense:

# >>> print select([func.to_json(User)]).select_from(User)

# SELECT to_json(:to_json_2) AS to_json_1

# FROM users

# But when I go to execute, I get:
session
.execute(query)

# sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type 'DeclarativeMeta' 'SELECT to_json(%(to_json_2)s) AS to_json_1 \nFROM users' {'to_json_2': <class 'monitoring.models.User'>}


Obviously I'm doing something terribly wrong. Might someone be able to point me in the right direction? I haven't been able to find anything in the docs that addresses trying to do such a thing. Oh, I've also tried passing the model's Table object, but that seems to be even worse. It's as if the parameter wasn't ever passed.

Thanks!

Mike Bayer

unread,
Aug 22, 2015, 12:51:53 PM8/22/15
to sqlal...@googlegroups.com
what's the SQL you want - is this the special Postgresql syntax where an entire table name is sent to a function?  SQLAlchemy doesn't support that.   Just use text(), if this is the case.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Tim Radke

unread,
Aug 22, 2015, 12:54:30 PM8/22/15
to sqlal...@googlegroups.com

That is the case, I'm afraid. Thanks for the prompt reply and thanks for making such a killer library. :-)


You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/nSjb2efvyvY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages