Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Databaseintermediate

ORM Model Generator

Share

Generate ORM models from database schema

Works with OpenClaude

You are a database architect and ORM specialist. The user wants to generate ORM models automatically from an existing database schema.

What to check first

  • Run SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'your_db' to verify database connectivity and list tables
  • Confirm which ORM framework you're targeting (SQLAlchemy, Django ORM, Sequelize, TypeORM, etc.)
  • Verify database driver is installed: pip list | grep sqlalchemy or npm list typeorm

Steps

  1. Connect to your database using the ORM's introspection/reflection tools (e.g., sqlacodegen for SQLAlchemy, inspectdb for Django, typeorm entity:create for TypeORM)
  2. Run the code generation command with your database connection string pointing to the target schema
  3. Specify output directory where generated model files will be written
  4. Configure model naming conventions (singular vs. plural, camelCase vs. snake_case)
  5. Review generated models for column types, nullable constraints, and foreign key relationships
  6. Add custom validators, properties, or business logic methods to generated classes
  7. Create an __init__.py or index file that imports and exports all generated models
  8. Test model instantiation and query methods against your actual database tables

Code

from sqlalchemy import create_engine, inspect
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
import inflect

Base = declarative_base()
engine = create_engine('postgresql://user:password@localhost/mydb')
inspector = inspect(engine)

def generate_models(schema='public'):
    """Generate SQLAlchemy ORM models from database schema."""
    p = inflect.engine()
    models_code = "from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey\n"
    models_code += "from sqlalchemy.ext.declarative import declarative_base\n"
    models_code += "from sqlalchemy.orm import relationship\n\n"
    models_code += "Base = declarative_base()\n\n"
    
    for table_name in inspector.get_table_names(schema=schema):
        columns = inspector.get_columns(table_name, schema=schema)
        pk = inspector.get_pk_constraint(table_name, schema=schema)
        fks = inspector.get_foreign_keys(table_name, schema=schema)
        
        # Class name: convert snake_case to PascalCase
        class_name = ''.join(word.capitalize() for word in table_name.split('_'))
        models_code += f"class {class_name}(Base):\n"
        models_code += f"    __tablename__ = '{table_name}'\n\n"
        
        # Generate columns
        for col in columns:
            col_name = col['name']
            col_type = str(col

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

Quick Info

CategoryDatabase
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
databaseormmodels

Install command:

curl -o ~/.claude/skills/orm-model-generator.md https://claude-skills-hub.vercel.app/skills/database/orm-model-generator.md

Related Database Skills

Other Claude Code skills in the same category — free to download.

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.