Writing Queries

Generate SQLAlchemy ORM models and repository patterns with session management.

1|2|Updated Feb 5, 2026
One-click install
npx skills add https://github.com/7a336e6e/skills --skill writing-queries-7a336e6e
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Writing Queries
Source: https://github.com/7a336e6e/skills/tree/main/database/writing-queries
Command: npx skills add https://github.com/7a336e6e/skills --skill writing-queries-7a336e6e

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill streamlines database interactions by providing a robust framework for writing efficient SQLAlchemy ORM queries and leveraging raw SQL for complex reporting needs, ensuring data logic is well-organized and performant.

Core Features & Use Cases

  • ORM Model Definition: Create SQLAlchemy models that accurately reflect database schemas.
  • Repository Pattern Implementation: Encapsulate all database operations within dedicated repository classes for better code organization.
  • Session Management: Ensure proper handling of database sessions with explicit commit and rollback boundaries.
  • Performance Optimization: Prevent N+1 query issues through eager loading techniques.
  • Hybrid Approach: Utilize raw SQL for complex queries where ORM syntax becomes cumbersome or inefficient.
  • Use Case: When developing a new feature that requires creating, reading, updating, and deleting user data, this Skill guides you through defining the User model, implementing a UserRepository, and managing sessions correctly to ensure data integrity and performance.

Quick Start

Use the writing-queries skill to create a SQLAlchemy model for a 'Product' table and implement a repository to handle its CRUD operations.

Frequently Asked Questions about Writing Queries

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

FAQPage Schema
How do I implement the repository pattern with SQLAlchemy for CRUD operations?

To implement the repository pattern with SQLAlchemy, you encapsulate all CRUD operations within dedicated repository classes. This organizes data access logic by defining ORM models and managing sessions with explicit commit and rollback boundaries.

How do I fix N+1 query issues in SQLAlchemy ORM?

Fix N+1 query issues in SQLAlchemy ORM by applying eager loading techniques. This performance optimization strategy loads related data upfront, preventing sequential database hits when accessing relationships within your data access layer.

When should I use raw SQL instead of SQLAlchemy ORM for queries?

Use raw SQL instead of SQLAlchemy ORM for complex reporting queries where ORM syntax becomes cumbersome or inefficient. This hybrid approach ensures maintainability and performance for demanding data extraction needs in Python applications.

What is the best way to manage SQLAlchemy database sessions in Python?

The best way to manage SQLAlchemy database sessions is by establishing explicit commit and rollback boundaries. Proper session handling ensures data integrity during CRUD operations within your repository pattern implementation.

Does the repository pattern work well with SQLAlchemy for complex data models?

The repository pattern works well with SQLAlchemy by encapsulating complex data model operations within dedicated classes. It structures data access for Python applications, ensuring maintainability while supporting ORM models and raw SQL fallbacks for complex reporting.