# go-gorm/gorm architecture

> Architecture diagram of the GitHub repository go-gorm/gorm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/go-gorm/gorm
- Repository: https://github.com/go-gorm/gorm

## Overview

GORM is a Go ORM that turns model-oriented API calls into database operations and maps query results back to Go values. The main workflow is to configure a query through the chainable API, finish it through CRUD operations, run registered callbacks to build and execute SQL, then scan results and process hooks or preloads. Separate branches cover association management, transactions, schema migration, SQL construction, logging, and prepared-statement caching. The tree and README establish these capabilities; sampled excerpts provide detailed evidence for query callbacks, schema metadata, migration, and statement caching, but not every callback’s wiring.

Main entry points: [`callbacks/query.go`](https://github.com/go-gorm/gorm/blob/master/callbacks/query.go) (Query callbacks), [`gorm.go`](https://github.com/go-gorm/gorm/blob/master/gorm.go) (GORM DB), [`callbacks/callbacks.go`](https://github.com/go-gorm/gorm/blob/master/callbacks/callbacks.go) (Callback registry), [`schema/schema.go`](https://github.com/go-gorm/gorm/blob/master/schema/schema.go) (Model schema)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["ORM API"]
  node_db["GORM DB<br/>[gorm.go]"]
  node_chain["Query builder API<br/>[chainable_api.go]"]
  node_finish["CRUD finishers<br/>[finisher_api.go]"]
  node_generic["Typed API<br/>[generics.go]"]
end

subgraph group_execution["SQL execution"]
  node_registry["Callback registry<br/>[callbacks.go]"]
  node_crud["CRUD callbacks<br/>[create.go]"]
  node_query["Query callbacks<br/>[query.go]"]
  node_preload["Preload processing<br/>[preload.go]"]
  node_tx["Transaction callbacks<br/>[transaction.go]"]
  node_statement["Statement builder<br/>[statement.go]"]
  node_clauses["SQL clauses<br/>[clause.go]"]
  node_scan["Result scanning<br/>[scan.go]"]
end

subgraph group_model["Models and relations"]
  node_schema["Model schema<br/>[schema.go]"]
  node_fields["Field mapping<br/>[field.go]"]
  node_relations["Relationships<br/>[relationship.go]"]
  node_associations["Association API<br/>[association.go]"]
end

subgraph group_operations["Database operations"]
  node_migration_api["Migration API<br/>[migrator.go]"]
  node_migration["Schema migration<br/>[migrator.go]"]
end

subgraph group_support["Runtime support"]
  node_logger["Logging<br/>[logger.go]"]
  node_prepared["Prepared statements<br/>[prepare_stmt.go]"]
  node_stmt_store["Statement cache<br/>[stmt_store.go]"]
  node_plugins["Plugin interfaces<br/>[interfaces.go]"]
end

node_user(("Application"))
node_sql_db[("SQL database")]

node_user -->|"opens database"| node_db
node_user -->|"configures query"| node_chain
node_chain -->|"finishes operation"| node_finish
node_generic -->|"delegates CRUD"| node_finish
node_db -->|"registers callbacks"| node_registry
node_finish -->|"executes callbacks"| node_registry
node_registry -->|"dispatches writes"| node_crud
node_registry -->|"dispatches reads"| node_query
node_registry -->|"dispatches transactions"| node_tx
node_crud -->|"builds statement"| node_statement
node_query -->|"builds statement"| node_statement
node_statement -->|"builds SQL"| node_clauses
node_query -->|"reads metadata"| node_schema
node_crud -->|"reads metadata"| node_schema
node_query -->|"queries"| node_sql_db
node_crud -->|"executes SQL"| node_sql_db
node_query -->|"scans rows"| node_scan
node_sql_db -->|"returns rows"| node_scan
node_query -->|"processes preloads"| node_preload
node_preload -->|"reads relationships"| node_relations
node_associations -->|"uses relationship metadata"| node_relations
node_associations -->|"runs association CRUD"| node_finish
node_tx -->|"controls transactions"| node_sql_db
node_user -->|"requests migration"| node_migration_api
node_migration_api -->|"delegates migration"| node_migration
node_migration -->|"parses models"| node_schema
node_migration -->|"alters schema"| node_sql_db
node_schema -->|"maps fields"| node_fields
node_schema -->|"derives relationships"| node_relations
node_db -.->|"logs operations"| node_logger
node_db -.->|"enables prepared mode"| node_prepared
node_prepared -->|"caches statements"| node_stmt_store
node_stmt_store -->|"prepares SQL"| node_sql_db
node_db -.->|"exposes extension API"| node_plugins

click node_db "https://github.com/go-gorm/gorm/blob/master/gorm.go"
click node_chain "https://github.com/go-gorm/gorm/blob/master/chainable_api.go"
click node_finish "https://github.com/go-gorm/gorm/blob/master/finisher_api.go"
click node_generic "https://github.com/go-gorm/gorm/blob/master/generics.go"
click node_registry "https://github.com/go-gorm/gorm/blob/master/callbacks/callbacks.go"
click node_crud "https://github.com/go-gorm/gorm/blob/master/callbacks/create.go"
click node_query "https://github.com/go-gorm/gorm/blob/master/callbacks/query.go"
click node_preload "https://github.com/go-gorm/gorm/blob/master/callbacks/preload.go"
click node_tx "https://github.com/go-gorm/gorm/blob/master/callbacks/transaction.go"
click node_statement "https://github.com/go-gorm/gorm/blob/master/statement.go"
click node_clauses "https://github.com/go-gorm/gorm/blob/master/clause/clause.go"
click node_scan "https://github.com/go-gorm/gorm/blob/master/scan.go"
click node_schema "https://github.com/go-gorm/gorm/blob/master/schema/schema.go"
click node_fields "https://github.com/go-gorm/gorm/blob/master/schema/field.go"
click node_relations "https://github.com/go-gorm/gorm/blob/master/schema/relationship.go"
click node_associations "https://github.com/go-gorm/gorm/blob/master/association.go"
click node_migration_api "https://github.com/go-gorm/gorm/blob/master/migrator.go"
click node_migration "https://github.com/go-gorm/gorm/blob/master/migrator/migrator.go"
click node_logger "https://github.com/go-gorm/gorm/blob/master/logger/logger.go"
click node_prepared "https://github.com/go-gorm/gorm/blob/master/prepare_stmt.go"
click node_stmt_store "https://github.com/go-gorm/gorm/blob/master/internal/stmt_store/stmt_store.go"
click node_plugins "https://github.com/go-gorm/gorm/blob/master/interfaces.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_db,node_chain,node_finish,node_generic toneBlue
class node_registry,node_crud,node_query,node_preload,node_tx,node_statement,node_clauses,node_scan,node_sql_db toneAmber
class node_schema,node_fields,node_relations,node_associations toneMint
class node_migration_api,node_migration toneRose
class node_logger,node_prepared,node_stmt_store,node_plugins,node_user toneIndigo
```

## Components

### ORM API

- [GORM DB](https://github.com/go-gorm/gorm/blob/master/gorm.go): `gorm.go`
- [Query builder API](https://github.com/go-gorm/gorm/blob/master/chainable_api.go): `chainable_api.go`
- [CRUD finishers](https://github.com/go-gorm/gorm/blob/master/finisher_api.go): `finisher_api.go`
- [Typed API](https://github.com/go-gorm/gorm/blob/master/generics.go): `generics.go`

### SQL execution

- [Callback registry](https://github.com/go-gorm/gorm/blob/master/callbacks/callbacks.go): `callbacks/callbacks.go`
- [CRUD callbacks](https://github.com/go-gorm/gorm/blob/master/callbacks/create.go): `callbacks/create.go`
- [Query callbacks](https://github.com/go-gorm/gorm/blob/master/callbacks/query.go): `callbacks/query.go`
- [Preload processing](https://github.com/go-gorm/gorm/blob/master/callbacks/preload.go): `callbacks/preload.go`
- [Transaction callbacks](https://github.com/go-gorm/gorm/blob/master/callbacks/transaction.go): `callbacks/transaction.go`
- [Statement builder](https://github.com/go-gorm/gorm/blob/master/statement.go): `statement.go`
- [SQL clauses](https://github.com/go-gorm/gorm/blob/master/clause/clause.go): `clause/clause.go`
- [Result scanning](https://github.com/go-gorm/gorm/blob/master/scan.go): `scan.go`

### Models and relations

- [Model schema](https://github.com/go-gorm/gorm/blob/master/schema/schema.go): `schema/schema.go`
- [Field mapping](https://github.com/go-gorm/gorm/blob/master/schema/field.go): `schema/field.go`
- [Relationships](https://github.com/go-gorm/gorm/blob/master/schema/relationship.go): `schema/relationship.go`
- [Association API](https://github.com/go-gorm/gorm/blob/master/association.go): `association.go`

### Database operations

- [Migration API](https://github.com/go-gorm/gorm/blob/master/migrator.go): `migrator.go`
- [Schema migration](https://github.com/go-gorm/gorm/blob/master/migrator/migrator.go): `migrator/migrator.go`

### Runtime support

- [Logging](https://github.com/go-gorm/gorm/blob/master/logger/logger.go): `logger/logger.go`
- [Prepared statements](https://github.com/go-gorm/gorm/blob/master/prepare_stmt.go): `prepare_stmt.go`
- [Statement cache](https://github.com/go-gorm/gorm/blob/master/internal/stmt_store/stmt_store.go): `internal/stmt_store/stmt_store.go`
- [Plugin interfaces](https://github.com/go-gorm/gorm/blob/master/interfaces.go): `interfaces.go`

### Other components

- **Application**
- **SQL database**

## Connections

- Application → GORM DB: opens database
- Application → Query builder API: configures query
- Query builder API → CRUD finishers: finishes operation
- Typed API → CRUD finishers: delegates CRUD
- GORM DB → Callback registry: registers callbacks
- CRUD finishers → Callback registry: executes callbacks
- Callback registry → CRUD callbacks: dispatches writes
- Callback registry → Query callbacks: dispatches reads
- Callback registry → Transaction callbacks: dispatches transactions
- CRUD callbacks → Statement builder: builds statement
- Query callbacks → Statement builder: builds statement
- Statement builder → SQL clauses: builds SQL
- Query callbacks → Model schema: reads metadata
- CRUD callbacks → Model schema: reads metadata
- Query callbacks → SQL database: queries
- CRUD callbacks → SQL database: executes SQL
- Query callbacks → Result scanning: scans rows
- SQL database → Result scanning: returns rows
- Query callbacks → Preload processing: processes preloads
- Preload processing → Relationships: reads relationships
- Association API → Relationships: uses relationship metadata
- Association API → CRUD finishers: runs association CRUD
- Transaction callbacks → SQL database: controls transactions
- Application → Migration API: requests migration
- Migration API → Schema migration: delegates migration
- Schema migration → Model schema: parses models
- Schema migration → SQL database: alters schema
- Model schema → Field mapping: maps fields
- Model schema → Relationships: derives relationships
- GORM DB → Logging: logs operations
- GORM DB → Prepared statements: enables prepared mode
- Prepared statements → Statement cache: caches statements
- Statement cache → SQL database: prepares SQL
- GORM DB → Plugin interfaces: exposes extension API
