ORM Generation
Last updated: March 2026
In addition to SQL DDL, diagram2code can generate ORM model code directly from your ER diagram. The generator maps the same intermediate schema model — tables, columns, types, relationships — to idiomatic model definitions in the target language and framework. In the converter, switch the Output selector to ORM Models and choose your target framework. Via the API, use the POST /v1/convert/orm endpoint with the framework field.
Supported ORM Frameworks
The table below lists all supported frameworks. GORM is available immediately with no sign-up. The remaining frameworks are available via the API with a Developer or Pro tier key, and progressively via the web converter as each generator is promoted out of beta.
| Framework | Language | File ext. | Tier |
|---|---|---|---|
| GORM v2 | Go | .go | Free |
| SQLAlchemy 2 | Python | .py | Developer+ |
| TypeORM | TypeScript | .ts | Developer+ |
| Prisma Schema | Prisma DSL | .prisma | Developer+ |
| JPA / Hibernate | Java | .java | Developer+ |
| Django ORM | Python | .py | Developer+ |
| Sequelize | JavaScript / TypeScript | .js | Developer+ |
| Entity Framework Core | C# | .cs | Developer+ |
GORM (Go) — Free
The GORM generator produces idiomatic Go source code compatible with GORM v2. All structs are placed in package models. Required imports (time, encoding/json) are added automatically when needed.
Struct and field naming:
- Table names are singularized and converted to PascalCase:
order_items→OrderItem - Column names are converted to PascalCase:
user_id→UserID,created_at→CreatedAt - Common initialisms are preserved:
id→ID,url→URL - Every field includes both a
gorm:"..."tag and ajson:"..."tag
Type Mapping
Schema abstract types map to Go types as follows. Nullable columns (no NOT NULL / no PK) become pointer types.
| Schema type | Go type | Notes |
|---|---|---|
int, integer, serial | uint | Unsigned; nullable → *uint |
bigint, bigserial | uint64 | Nullable → *uint64 |
smallint | int16 | Nullable → *int16 |
tinyint | int8 | Nullable → *int8 |
string, varchar, char, text, clob | string | Nullable → *string |
decimal, numeric, float, double, number | float64 | Nullable → *float64 |
real | float32 | Nullable → *float32 |
boolean, bool, bit | bool | Nullable → *bool |
date, datetime, timestamp, timestamptz | time.Time | Requires import "time"; nullable → *time.Time |
uuid, uniqueidentifier | string | UUID represented as string |
blob, bytea | []byte | Binary data |
json, jsonb | json.RawMessage | Requires import "encoding/json" |
enum (with values) | Named type + constants | See Enums below |
Relationships
Relationships detected from FK columns and diagram cardinality markers are represented as navigation fields:
| Relationship | Parent side | Child side |
|---|---|---|
| One-to-many | Orders []Order `gorm:"foreignKey:UserID"` | User User `gorm:"constraint:OnDelete:CASCADE"` |
| One-to-one | Profile Profile `gorm:"foreignKey:UserID"` | User User `gorm:"constraint:OnDelete:CASCADE"` |
| Many-to-many | Tags []Tag `gorm:"many2many:product_tags"` | Products []Product `gorm:"many2many:product_tags"` |
Many-to-many join table names are auto-generated as from_table_to_table (lowercase, alphabetical order).
Enums
When a column has an enum type with defined values, the generator produces a named string type and typed constants:
erDiagram
ORDER {
int id PK
enum status "active,inactive,completed"
}type OrderStatus string
const (
OrderStatusActive OrderStatus = "active"
OrderStatusInactive OrderStatus = "inactive"
OrderStatusCompleted OrderStatus = "completed"
)
type Order struct {
ID uint `gorm:"column:id;primaryKey" json:"id"`
Status OrderStatus `gorm:"column:status;type:enum(active,inactive,completed)" json:"status"`
}Full Example: GORM Model Generation
The same USER / ORDER diagram used in the SQL examples, converted to GORM Go models:
erDiagram
USER {
int id PK
string name
string email
}
ORDER {
int id PK
int user_id FK
decimal total
}
USER ||--o{ ORDER : placespackage models
type User struct {
ID uint `gorm:"column:id;primaryKey" json:"id"`
Name *string `gorm:"column:name" json:"name"`
Email *string `gorm:"column:email" json:"email"`
Orders []Order `gorm:"foreignKey:UserID" json:"orders,omitempty"`
}
type Order struct {
ID uint `gorm:"column:id;primaryKey" json:"id"`
UserID *uint `gorm:"column:user_id" json:"user_id"`
Total *float64 `gorm:"column:total" json:"total"`
User User `gorm:"constraint:OnDelete:CASCADE" json:"user,omitempty"`
}*string, *uint) to correctly represent nullable columns and distinguish zero values from missing values in Go.