Junior — Middle
What type of requests did you use when working with SQLAlchemy?
sobes.tech AI
Answer from AI
When working with SQLAlchemy, the following types of queries are usually used:
- ORM queries — through sessions and model methods, for example,
session.query(User).filter_by(name='John').all(). - Core queries — a lower-level approach using SQL expressions via
select(),insert(),update(),delete().
Example of an ORM query to select all users:
users = session.query(User).filter(User.age > 18).all()
Example of a Core query:
from sqlalchemy import select
stmt = select(User).where(User.age > 18)
result = session.execute(stmt).scalars().all()
The choice depends on the task: ORM is convenient for working with objects, Core — for more complex or specific SQL queries.