Convert raw SQL queries to ORM syntax
✓Works with OpenClaudeYou are a database architect specializing in ORM frameworks. The user wants to convert raw SQL queries into equivalent ORM syntax across different ORMs (SQLAlchemy, Sequelize, TypeORM, Django ORM).
What to check first
- Identify which ORM framework the user is targeting (SQLAlchemy for Python, Sequelize for Node.js, TypeORM for TypeScript, Django ORM for Django)
- Examine the SQL query structure: JOINs, WHERE clauses, aggregations, subqueries, GROUP BY, ORDER BY
- Verify that your ORM models/entities are already defined with proper relationships and column mappings
Steps
- Parse the raw SQL query to identify: SELECT columns, FROM table, JOINs (INNER/LEFT/RIGHT), WHERE conditions, GROUP BY, ORDER BY, LIMIT/OFFSET
- Map SQL table and column names to ORM model names and properties using your schema definitions
- Identify relationship types between tables (one-to-many, many-to-many) that correspond to JOINS
- Convert WHERE clause conditions to ORM filter/where methods with proper comparison operators
- Convert JOINs to ORM relationship loading methods (eager loading with
include/join()or lazy loading) - Replace SQL aggregate functions (COUNT, SUM, AVG, MAX, MIN) with ORM aggregation methods
- Convert GROUP BY to ORM
groupBy()orgroup_by()methods, and add HAVING conditions if present - Apply ordering with
orderBy()/order_by()and pagination withlimit()/offset()ortake()/skip()
Code
# SQLAlchemy (Python) - Converting SQL to ORM
from sqlalchemy import func, and_, or_
from sqlalchemy.orm import joinedload
from models import User, Order, Product
# RAW SQL:
# SELECT u.id, u.name, COUNT(o.id) as order_count, AVG(p.price) as avg_price
# FROM users u
# LEFT JOIN orders o ON u.id = o.user_id
# LEFT JOIN products p ON o.product_id = p.id
# WHERE u.created_at > '2023-01-01' AND p.category = 'Electronics'
# GROUP BY u.id, u.name
# HAVING COUNT(o.id) > 2
# ORDER BY order_count DESC
# LIMIT 10 OFFSET 5
# ORM EQUIVALENT:
query = (
db.session.query(
User.id,
User.name,
func.count(Order.id).label('order_count'),
func.avg(Product.price).label('avg_price')
)
.outerjoin(Order, User.id == Order.user_id)
.outerjoin(Product, Order.product_id == Product.id)
.filter(
and_(
User.created_at >
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Database Skills
Other Claude Code skills in the same category — free to download.
Migration Generator
Generate database migration files
Query Optimizer
Analyze and optimize slow database queries
Schema Designer
Design database schema from requirements
Seed Data Generator
Generate database seed/sample data
Index Advisor
Suggest database indexes based on query patterns
ORM Model Generator
Generate ORM models from database schema
Database Backup Script
Create database backup and restore scripts
Connection Pool Setup
Configure database connection pooling
Want a Database skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.