0

I'm currently executing this query in one process:

SELECT DISTINCT ON (c.api_key, worker_id) worker_id, c.api_key, a.updated_at, b.user_id, a.country
           FROM TABLE_A a
               INNER JOIN TABLE_B b ON (b.id = a.user)
               INNER JOIN TABLE_C c ON (b.owner = c.id)
           WHERE 1=1 
               AND a.platform = 'x'
               AND a.country = 'y'
               AND a.access_token is not NULL
               
                      
           ORDER BY c.api_key, worker_id, a.updated_at desc

I'm currently wrapping it using from SQLAlchemy import text and then simply executing

query_results = db.execute(query).fetchall()
list_dicts = [r._asdict() for r in query_results]
df = pd.DataFrame(list_dicts)

and it works, but I would really like to see if it's possible to have it in the other notation, like :

db.query(TABLE_A).filter().join()... etc
2
  • Have you created SQLAlchemy models to map your tables? Commented Jan 21, 2022 at 12:02
  • Yes. You can actually use db.query(TABLE_A).filter()...etc Commented Jan 21, 2022 at 23:30

1 Answer 1

1

Yes, it's possible.

But the exact way to do it will depend on your SQLAlchmey version and how you've setup your SQLAlchemy project and models.

You may want to check out the SQLAlchemy ORM querying guide and the Expression Language Tutorial to see which one fits better your case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.