2

I am porting my old code where I had used mysql library for querying mysql. I am tyring to use flask_SQLALchemy to do the same but unable to find alternate option to query 'or'.

Below is a snippet from updated code on how it's working.

from flask_sqlalchemy import SQLAlchemy                                                                                                                                                        
from myapp import APP                                                                                                                                                                          
from myapp.models.nodes import Host                                                                                                                                                            


DB = SQLAlchemy(APP)  
# config is taken care here                                                                                                                                                                         

APP.app_context().push()                                                                                                                                                                       
with APP.app_context():                                                                                                                                                                        
    DB.init_app(APP)     

Hostname_var = "some hostname"
session_query = Host.query.filter_by(hostname=hostname_var).first()

Similarily, I want to query statement given below. I could use sqlalchemy but using different libraries doesn't sound ideal.

"Select count(*) as c from table_name where ver='some version' and arch='some arch' and pool=1 and (state='Ready' or state='Provisioning');" table_name's class is called Host (as used in example).

Any help is appreciated, thanks.

1 Answers1

7

You need to use | and &, such as:

Host.query.filter(
    (Host.ver == 'some version') & (Host.arch == 'some arch') & (Host.pool == 1) &
    ((Host.state == 'Ready') | (Host.state == 'Provisioning'))
).count()

would give you desired result.

vishes_shell
  • 22,409
  • 6
  • 71
  • 81