Skillshub generating-orm-code
git clone https://github.com/ComeOnOliver/skillshub
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/jeremylongshore/claude-code-plugins-plus-skills/generating-orm-code" ~/.claude/skills/comeonoliver-skillshub-generating-orm-code && rm -rf "$T"
skills/jeremylongshore/claude-code-plugins-plus-skills/generating-orm-code/SKILL.mdORM Code Generator
Overview
Generate type-safe ORM model classes, migration files, and repository patterns from existing database schemas or domain specifications. Supports Prisma, TypeORM, Sequelize, SQLAlchemy, Django ORM, and Drizzle ORM.
Prerequisites
- Database connection string or credentials for schema introspection
orpsql
CLI for queryingmysqlinformation_schema- Target ORM framework already installed in the project (
,prisma
,typeorm
, etc.)sqlalchemy - Node.js/Python/Go runtime matching the target ORM
- Existing project structure to place generated models in the correct directory
Instructions
-
Introspect the database schema by querying
,information_schema.COLUMNS
, andinformation_schema.TABLE_CONSTRAINTS
to extract all tables, columns, data types, nullable flags, defaults, primary keys, foreign keys, and unique constraints.information_schema.KEY_COLUMN_USAGE -
For PostgreSQL, additionally query
for custom enum types andpg_catalog.pg_type
for index definitions. For MySQL, querypg_catalog.pg_index
for index details.information_schema.STATISTICS -
Map database column types to ORM field types:
->varchar/text
/String@Column('text')
->integer/bigint
/Int@Column('int')
->boolean
/Boolean@Column('boolean')
->timestamp/datetime
/DateTime@Column('timestamp')
->jsonb/json
/Json@Column('jsonb')
->uuid
withString
or@default(uuid())uuid.uuid4- Custom enums -> Generate enum type definitions
-
Generate model classes with proper decorators/attributes:
- For Prisma: Generate
withschema.prisma
blocks,model
,@id
,@unique
, and@relation
directives.@default - For TypeORM: Generate entity classes with
,@Entity()
,@Column()
,@PrimaryGeneratedColumn()
,@ManyToOne()
decorators.@OneToMany() - For SQLAlchemy: Generate model classes extending
withBase
,Column()
,ForeignKey()
, andrelationship()
.__tablename__ - For Drizzle: Generate table definitions with
,pgTable()
,serial()
,varchar()
, andtimestamp()
.relations()
- For Prisma: Generate
-
Generate relationship mappings from foreign key constraints. Detect one-to-one (unique FK), one-to-many, and many-to-many (junction table with two FKs) patterns automatically. Add both sides of each relationship with proper cascade options.
-
Create migration files that capture the current schema state. For Prisma:
. For TypeORM: generate migration withnpx prisma migrate dev --name init
. For Alembic:typeorm migration:generate
.alembic revision --autogenerate -
Generate repository/service layer with common CRUD operations:
,findById
with pagination,findAll
,create
,update
, and relationship-aware queries (delete
).findWithRelations -
Add validation decorators or constraints matching database CHECK constraints and NOT NULL columns. Use
for TypeORM, Pydantic validators for SQLAlchemy, or Zod schemas for Prisma.class-validator -
Generate TypeScript/Python type definitions or interfaces for API layer consumption, ensuring the ORM models and API types stay synchronized.
-
Validate generated models by running a test migration against a temporary database or by comparing the generated schema against the live database schema with a diff tool.
Output
- Model/entity files with full type annotations, decorators, and relationship mappings
- Migration files capturing the initial schema state
- Enum type definitions for database enum columns
- Repository/service classes with typed CRUD operations
- Validation schemas (Zod, class-validator, Pydantic) matching database constraints
- Type definition files for API layer consumption
Error Handling
| Error | Cause | Solution |
|---|---|---|
| Circular relationship dependency | Two entities reference each other, causing import cycles | Use lazy loading () in TypeORM; use in SQLAlchemy; split into separate files with deferred imports |
| Unknown column type mapping | Database uses custom types, extensions, or domain types not in the standard mapping | Add custom type mapping in generator config; use as fallback; register custom transformers |
| Migration conflicts with existing data | Generated migration adds NOT NULL columns without defaults | Add default values to new columns; create a two-phase migration (add nullable, backfill, set NOT NULL) |
| Junction table not detected as many-to-many | Junction table has extra columns beyond the two foreign keys | Model as an explicit entity with two ManyToOne relationships instead of an implicit ManyToMany |
| Schema drift between ORM models and database | Manual database changes not reflected in ORM code | Run introspection again; use or to regenerate; diff against existing models |
Examples
Prisma schema from PostgreSQL e-commerce database: Introspect 15 tables including users, orders, products, and categories. Generate
schema.prisma with proper @relation directives, enum types for order status, and @default(autoincrement()) for serial columns. Output includes Zod validation schemas for each model.
TypeORM entities from MySQL SaaS application: Generate entity classes for a multi-tenant application with tenant isolation. Each entity includes a
tenantId column with a custom @TenantAware decorator. Repository layer includes tenant-scoped query methods.
SQLAlchemy models from legacy database with naming conventions: Introspect a database with inconsistent naming (mix of camelCase and snake_case). Generate models with
__tablename__ preserving original names while using Pythonic property names. Alembic migration captures the full schema.
Resources
- Prisma introspection: https://www.prisma.io/docs/orm/prisma-schema/introspection
- TypeORM entity documentation: https://typeorm.io/entities
- SQLAlchemy ORM tutorial: https://docs.sqlalchemy.org/en/20/orm/
- Drizzle ORM schema: https://orm.drizzle.team/docs/sql-schema-declaration
- Django inspectdb command: https://docs.djangoproject.com/en/5.0/howto/legacy-databases/