Can't find anything equivalent to bit_or in PostgreSQL

11 views
Skip to first unread message

Balukrishnan

unread,
Jun 16, 2020, 4:33:03 PM6/16/20
to sqlalchemy
Hi friends,

I am looking for function or operator which is equivalent to bit_or in PostgreSQL aggregate function. Is there any? I can't find any. Please help.

Jonathan Vanasco

unread,
Jun 16, 2020, 5:42:04 PM6/16/20
to sqlalchemy
If this just needs this to be rendered for PostgreSQL, you can use the `func` generator:

https://docs.sqlalchemy.org/en/13/core/sqlelement.html#sqlalchemy.sql.expression.func

from sqlalchemy.sql.expression import func

query
= session.query(Foo).filter(func.bit_or(Foo.cola, Foo.colb)...


`func` is a special namespace and will render UNKNOWN functions as you invoke them.

This simple solution to render the correct sql works for most people.

If you have more specific needs, such as using this within python comparisons, you will need to read the docs on Custom types (https://docs.sqlalchemy.org/en/13/core/custom_types.html); if you need this to work on multiple different databases you may have to write a custom compiler for each supported one (https://docs.sqlalchemy.org/en/13/core/compiler.html)




Balukrishnan

unread,
Jun 17, 2020, 1:55:14 AM6/17/20
to sqlalchemy
Thank you very much. I tried that and it is working perfectly for me.
Reply all
Reply to author
Forward
0 new messages