Flask Data Persistence
Managing state is at the heart of any web app. This skill focuses on the SQLAlchemy ORM.
Models & relationships
- Declarative Base: Defining tables as Python classes.
- Relationships: Using
db.relationshipfor One-to-Many and Many-to-Many mappings.
Migrations (Flask-Migrate)
Never manually edit your database schema. Use migrations.
- init:
flask db init - generate:
flask db migrate -m "Added user bio" - apply:
flask db upgrade
Performance
- Lazy Loading: Understanding
select,joined, andsubqueryloading strategies to avoid N+1 problems. - Pagination: Use
paginate()for manageable data display.
Best Practices
- Transactions: Ensure atomic updates within a single request context.
- Sensitive Data: Hash passwords before storing using
WerkzeugorPasslib.