0

I am having issues ietrating over results from SQL query with using encode databases (https://pypi.org/project/databases/) but this sqlalchemy query works fine for the celery tasks

query = session.query(orders).filter(orders.shipped == True)

I have tried the following (celery task unable to iterate over multiple rows from postgresql database with python) but does not work

def check_all_orders():
    query = "SELECT * FROM orders WHERE shipped=True"
    return database.fetch_all(query)

...
...
...

@app.task
async def check_orders():

    query = await check_all_orders()
    
    today = datetime.utcnow()

    for q in query:
        if q.last_notification is not None:
            if (today - q.last_notification).total_seconds() < q.cooldown:
                continue

Does anyone know what SQL statement will generate what i can iterate like it does for this sqlalchemy query?

query = session.query(orders).filter(orders.shipped == True)
DejanLekic
  • 18,787
  • 4
  • 46
  • 77
uberrebu
  • 3,597
  • 9
  • 38
  • 73

1 Answers1

0

SQLAlchemy's resulting query depends on what particular backend engine you use. For example, filter(orders.shipped == True) can be converted to something like WHERE shipped = 't' for PostgreSQL. You can always log the query it sends to database backend. For your particular case SELECT * FROM orders WHERE shipped should be enough.

Denis Otkidach
  • 32,032
  • 8
  • 79
  • 100
  • issue is celery tasks doesnt work for me...it works for the sqlalchemy query but not for the databases package am using with raw sql statement..check this https://stackoverflow.com/questions/67507520 so what am trying to do now is to be able to get same result as sqlalchemy and then am good, if result is same..am using https://pypi.org/project/databases/ – uberrebu May 14 '21 at 15:37