sql-and-database

Provide SQLAlchemy patterns for safe database operations in Python.

Updated Apr 20, 2025
One-click install
npx skills add https://github.com/cathayrisk/Anya --skill sql-and-database-cathayrisk
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-and-database
Source: https://github.com/cathayrisk/Anya/tree/main/skills/sql-and-database
Command: npx skills add https://github.com/cathayrisk/Anya --skill sql-and-database-cathayrisk

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Many Python projects mishandle database interactions, causing connection leaks, SQL injection risks, inefficient queries, and brittle migrations; this skill consolidates best practices for safe, predictable database access and ORM usage so developers can read and write data reliably.

Core Features & Use Cases

  • Engine and connection pooling patterns including singletons and SQLite development config to avoid resource exhaustion.
  • SQLAlchemy ORM model design, relationship handling, and metadata management for robust schema definitions.
  • CRUD and transaction patterns using Session context managers, session.commit/refresh semantics, and soft-delete recommendations.
  • Secure raw SQL usage with parameterized queries to prevent SQL injection and examples of migrations guidance (use Alembic in production).
  • Pandas ↔ database workflows for bulk inserts and reads, and async database access patterns using async SQLAlchemy and AsyncSession.
  • Common pitfalls and mitigations such as N+1 query avoidance, expire_on_commit implications, and thread-safety considerations.

Quick Start

Create a single Engine with get_engine(), use with Session() context managers, and execute parameterized queries to fetch or persist user data safely.

Frequently Asked Questions about sql-and-database

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent SQL injection in Python when executing raw SQL queries?

Prevent SQL injection in Python raw SQL queries by using parameterized queries with bound parameters instead of string interpolation. This skill provides secure execution patterns that separate query structure from user-supplied data to block injection attacks.

What's the best way to manage SQLAlchemy sessions and connection pooling?

Manage SQLAlchemy sessions and connection pooling by creating a single Engine via get_engine() and using Session() context managers. This skill demonstrates singleton engine patterns and session commit semantics to prevent connection leaks and ensure predictable transactions.

How do I avoid N+1 query problems in SQLAlchemy ORM mappings?

Avoid N+1 query problems in SQLAlchemy ORM mappings by applying eager loading strategies and proper relationship handling. This skill covers common pitfalls like N+1 queries, expire_on_commit implications, and thread-safety considerations with mitigation patterns.

Can I use pandas DataFrames for bulk inserts and reads with a PostgreSQL database?

Use pandas DataFrames for bulk inserts and reads with PostgreSQL, SQLite, or MySQL databases. This skill provides pandas-to-database workflow patterns for efficient bulk data operations and query result ingestion directly into DataFrame structures.

Does async SQLAlchemy support AsyncSession for non-blocking database operations?

Async SQLAlchemy supports AsyncSession for non-blocking database operations in Python applications. This skill provides async database access patterns using async drivers and AsyncSession to handle concurrent queries without blocking the event loop.

Why do I need Alembic for database migrations instead of raw SQL scripts?

Alembic is needed for database migrations in production to manage schema changes safely and track version history. This skill recommends using Alembic for production migrations rather than raw SQL scripts to ensure reliable, reversible schema evolution.