SQL → GORM
Golang ORM & Database Modeling

SQL DDL to Golang GORM Struct Model Generator

Translate SQL CREATE TABLE schemas into Golang struct definitions with GORM tags, JSON tags, and proper Go type mappings.

100% Client-Side Privacy
Zero Server Transmission
Presets:
GORM Model Compiler Options & Schema Mapping
auto•pkg models•*Pointers
SQL DDL Schema(3 tables)
// Code generated by ToolsAA SQL to Golang GORM Generator. DO NOT EDIT.
package models

import (
	"github.com/google/uuid"
	"gorm.io/datatypes"
	"gorm.io/gorm"
	"time"
)

// ============================================================================
// Custom Enum Definitions
// ============================================================================

type UserRole string

const (
	UserRoleOwner UserRole = "OWNER"
	UserRoleAdmin UserRole = "ADMIN"
	UserRoleMember UserRole = "MEMBER"
	UserRoleGuest UserRole = "GUEST"
)

type SubscriptionTier string

const (
	SubscriptionTierFree SubscriptionTier = "FREE"
	SubscriptionTierPro SubscriptionTier = "PRO"
	SubscriptionTierEnterprise SubscriptionTier = "ENTERPRISE"
)

// Organization maps to database table "organizations"
type Organization struct {
	ID                   *uuid.UUID       `gorm:"column:id;primaryKey;type:uuid;default:gen_random_uuid()" json:"id,omitempty"`
	Slug                 string           `gorm:"column:slug;type:varchar(80);not null;uniqueIndex:idx_organizations_slug" json:"slug"`
	Name                 string           `gorm:"column:name;type:varchar(120);not null" json:"name"`
	Tier                 SubscriptionTier `gorm:"column:tier;type:varchar(64);not null;default:FREE" json:"tier"`
	MaxSeats             int              `gorm:"column:max_seats;not null;default:5" json:"max_seats"`
	IsActive             bool             `gorm:"column:is_active;type:boolean;not null;default:true" json:"is_active"`
	Settings             *datatypes.JSON  `gorm:"column:settings;type:jsonb" json:"settings,omitempty"`
	CreatedAt            time.Time        `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt            time.Time        `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP" json:"updated_at"`
	DeletedAt            gorm.DeletedAt   `gorm:"column:deleted_at;index" json:"deleted_at,omitempty"`
}

// TableName specifies the database table name for Organization
func (Organization) TableName() string {
	return "organizations"
}

// User maps to database table "users"
type User struct {
	ID                   *uuid.UUID       `gorm:"column:id;primaryKey;type:uuid;default:gen_random_uuid()" json:"id,omitempty"`
	OrganizationID       uuid.UUID        `gorm:"column:organization_id;type:uuid;not null;index:idx_users_org" json:"organization_id"`
	Email                string           `gorm:"column:email;type:varchar(255);not null;uniqueIndex:idx_users_email" json:"email"`
	FullName             *string          `gorm:"column:full_name;type:varchar(120)" json:"full_name,omitempty"`
	Role                 UserRole         `gorm:"column:role;type:varchar(64);not null;default:MEMBER" json:"role"`
	AvatarURL            *string          `gorm:"column:avatar_url;type:varchar(500)" json:"avatar_url,omitempty"`
	IsVerified           bool             `gorm:"column:is_verified;type:boolean;not null;default:false" json:"is_verified"`
	Preferences          *datatypes.JSON  `gorm:"column:preferences;type:jsonb" json:"preferences,omitempty"`
	CreatedAt            time.Time        `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt            time.Time        `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP" json:"updated_at"`
	DeletedAt            gorm.DeletedAt   `gorm:"column:deleted_at;index" json:"deleted_at,omitempty"`
	Organization         *Organization    `gorm:"foreignKey:OrganizationID;references:ID;constraint:OnDelete:CASCADE" json:"organizations,omitempty"`
}

// TableName specifies the database table name for User
func (User) TableName() string {
	return "users"
}

// AuditLog maps to database table "audit_logs"
type AuditLog struct {
	ID                   *int64           `gorm:"column:id;primaryKey;autoIncrement;type:bigint" json:"id,omitempty"`
	OrganizationID       uuid.UUID        `gorm:"column:organization_id;type:uuid;not null" json:"organization_id"`
	UserID               *uuid.UUID       `gorm:"column:user_id;type:uuid" json:"user_id,omitempty"`
	Action               string           `gorm:"column:action;type:varchar(100);not null;index:idx_audit_logs_action" json:"action"`
	IPAddress            *string          `gorm:"column:ip_address;type:varchar(45)" json:"ip_address,omitempty"`
	UserAgent            *string          `gorm:"column:user_agent;type:text" json:"user_agent,omitempty"`
	Payload              *datatypes.JSON  `gorm:"column:payload;type:jsonb" json:"payload,omitempty"`
	CreatedAt            time.Time        `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	Organization         *Organization    `gorm:"foreignKey:OrganizationID;references:ID;constraint:OnDelete:CASCADE" json:"organizations,omitempty"`
	User                 *User            `gorm:"foreignKey:UserID;references:ID;constraint:OnDelete:SET NULL" json:"users,omitempty"`
}

// TableName specifies the database table name for AuditLog
func (AuditLog) TableName() string {
	return "audit_logs"
}
Zero-Allocation Structs

Pure Go structs mapped with precise memory layout, primitive sizing, and idiomatic field tags.

GORM v2 Tags & Associations

Full support for primaryKey, foreignKey, autoIncrement, uniqueIndex, and custom TableName() methods.

Type-Safe Nullability

Configurable pointer or sql.Null* types ensuring zero silent nil pointer dereference panics.

Zero-Server Privacy Sandbox

All SQL AST parsing and Golang code synthesis run 100% locally in your browser memory. No database schema or proprietary enterprise data is ever transmitted.

Multi-Dialect GORM v2 Precision

Comprehensive type mapping for PostgreSQL (UUID, JSONB, TIMESTAMPTZ), MySQL (AUTO_INCREMENT, DATETIME, Decimal), and SQLite (INTEGER PRIMARY KEY).

Production Ready Architecture

Outputs complete Go packages with idiomatic initialisms (ID, URL, UUID), GORM repository CRUD methods, transaction helpers, and model hooks.

Technical Reference & Architecture Guide

SQL DDL to Golang GORM Struct Model Generator — Reference Guide

Translate SQL CREATE TABLE schemas into Golang struct definitions with GORM tags, JSON tags, and proper Go type mappings.

Key Parameters & Controls

SQL DialectEnum (MySQL | PostgreSQL | SQLite | SQL Server)

Target SQL dialect used to parse DDL syntax, data types, and primary key constraints.

Default: MySQL
Nullable Field MappingEnum (Pointers | sql.Null* | Primitive)

Strategy for mapping nullable SQL columns into Go struct fields (pointers like *string vs sql.NullString).

Default: Pointers (*T)
Struct Tag GeneratorsMulti-Select (gorm, json, db, yaml)

List of struct field tags to append to each generated model attribute.

Default: gorm, json
Embed gorm.ModelBoolean

Automatically embed gorm.Model when id, created_at, updated_at, and deleted_at columns are detected.

Default: true
Generate TableName()Boolean

Generate a TableName() receiver method on the struct to enforce exact database table names.

Default: true

Pro Tips & Best Practices

Leverage gorm.Model Soft Deletes

When your schema contains deleted_at or gorm.DeletedAt timestamp columns, enabling gorm.Model embedding enables GORM soft deletes automatically.

Precise Temporal Mappings with time.Time

Map TIMESTAMP, DATETIME, and TIMESTAMPTZ columns to time.Time or *time.Time with gorm:"type:timestamp" tags to ensure seamless timezone parsing.

Dual Tagging for Clean REST & DB Layers

Combine GORM column annotations with json:"field_name,omitempty" tags to guarantee consistent snake_case or camelCase serialization across your API boundary.

PostgreSQL Dialect and JSONB Support

When targeting PostgreSQL, map JSON and JSONB columns to gorm.io/datatypes.JSON to access built-in GORM JSON querying and validation helpers.

Common Gotchas & Troubleshooting

Unsigned integers or BIGINT mapped to standard 32-bit int

Verify the SQL dialect setting; ensure BIGINT UNSIGNED maps to uint64 and INT UNSIGNED maps to uint32 to prevent runtime integer truncation.

Foreign key definitions not generating relationship references

Ensure FOREIGN KEY constraints use explicit REFERENCES table(column) syntax so the parser can generate associated struct pointers and foreignKey tags.

Special characters or reserved SQL keywords in column names

Wrap column identifiers in quotes or backticks; the parser automatically strips delimiters while setting gorm:"column:reserved_word".

Frequently Asked Questions

How does this generator handle nullable SQL columns?▼

Columns lacking a NOT NULL constraint can be mapped to Go pointers (e.g., *string, *int64) or database/sql helper types (e.g., sql.NullString, sql.NullTime).

Does the output conform to GORM v2 syntax?▼

Yes. Generated tags follow GORM v2 specifications including primaryKey, autoIncrement, type:, column:, and index tags.

Why is the TableName() method recommended?▼

By default, GORM pluralizes struct names to determine table names. Generating TableName() guarantees your struct maps to the exact table name defined in your DDL.

Is any SQL schema or database structure sent to a remote server?▼

No. All SQL parsing, tokenization, and Go code synthesis occur strictly in-browser via JavaScript WebAssembly/AST engines with zero network uploads.

Related Tools & Utilities

All Tools →