Home / Learn Secure SQL / SQLAlchemy: Core and ORM Layers
SQLAlchemy: Core and ORM Layers
Two APIs, both safe when used as intended.
Theory in plain words
The basic idea
SQLAlchemy offers a lower-level "Core" API (building SQL expressions in Python) and a higher-level ORM; both bind parameters automatically when you use their expression-building functions, including text() with bound params for raw SQL.
Why this comes up
SQLAlchemy's text() function is explicitly designed to accept raw SQL with named bound parameters safely, which is the right tool when the ORM's own query API is not expressive enough.
The mechanics
Use the ORM query API or Core expressions for most queries; for raw SQL, use sqlalchemy.text("... WHERE email = :email") with .execute({"email": email}), never Python string formatting.
Where it is used
- Web application code review
- Backend development
- Database administration
from sqlalchemy import text
result = connection.execute(
text('SELECT * FROM users WHERE email = :email'),
{'email': email}
)Quick check
1. SQLAlchemy's text() function is meant for...
Safe raw SQL with named params.