SQL to GORM Model Generator

Convert SQL CREATE TABLE statements to Go GORM model struct definitions with proper tags, type mapping, and TableName methods.

What is SQL to GORM Model Generator?

GORM is the most widely used ORM library in the Go ecosystem, providing a developer-friendly API for database operations with support for MySQL, PostgreSQL, SQLite, and SQL Server. It offers auto-migration, association handling, hooks, transactions, and composable scopes. This converter transforms your SQL CREATE TABLE statements into Go struct definitions with GORM struct tags that specify column names, types, constraints, and defaults, along with optional JSON tags for API serialization and TableName() methods for explicit table mapping.

How to Use

  1. Set the Go package name for the generated model file (default: "model")
  2. Toggle "GORM tags" to include gorm:"column:...;type:...;not null" struct tags for schema mapping
  3. Toggle "JSON tags" to include json:"field_name" tags for API serialization with encoding/json
  4. Toggle "TableName() method" to generate explicit table name mappings that override GORM naming conventions
  5. Paste your SQL CREATE TABLE statements and click "Generate GORM Model"
  6. Copy the output and save it as a .go file in your Go project

Why Use This Tool?

Generates Go structs with GORM tags that precisely map to your existing SQL schema columns, types, and constraints
Nullable SQL columns produce pointer types (*int, *time.Time) so you can distinguish between zero values and NULL in Go
JSON columns map to datatypes.JSON from gorm.io/datatypes, enabling flexible semi-structured data storage
JSON tags enable seamless marshaling and unmarshaling for REST API responses without additional mapping code
The TableName() method prevents GORM from incorrectly pluralizing or transforming your table names

Tips & Best Practices

  • Nullable columns without NOT NULL are generated as pointer types — this is essential for distinguishing between a zero value and a database NULL
  • JSON and JSONB columns use datatypes.JSON from gorm.io/datatypes, which implements the sql.Scanner and driver.Valuer interfaces for automatic serialization
  • Always generate the TableName() method when your Go struct names differ from your SQL table names, since GORM defaults to pluralized snake_case
  • You can customize the package name to match your project structure — common choices are model, models, domain, or entity

Frequently Asked Questions

How are SQL types mapped to Go types in GORM?

SQL types are mapped to Go types based on semantic meaning: VARCHAR/CHAR/TEXT become string, INT/INTEGER become int, BIGINT becomes int64, SMALLINT becomes int16, TINYINT becomes int8, DECIMAL/NUMERIC/FLOAT/DOUBLE become float64, BOOLEAN becomes bool, DATETIME/TIMESTAMP/DATE become time.Time, BLOB/BINARY become []byte, JSON/JSONB become datatypes.JSON, UUID becomes string. Nullable columns use pointer types.

When should I avoid using GORM?

GORM adds abstraction overhead that may not suit performance-critical applications. Avoid it when you need fine-grained control over SQL queries, when working with complex analytical queries, or when the magic behavior (auto-migration, hook ordering) creates more confusion than value. For those cases, consider sqlx, pgx, or Ent.

Why are some fields pointer types?

Nullable columns (those without NOT NULL and not primary keys) are generated as pointer types like *int, *time.Time, or *string. This lets you distinguish between a Go zero value (0, "") and a database NULL, which is critical for correct data handling in Go where zero values have meaning.

What does the gorm struct tag contain?

Each GORM tag includes column name, type, and constraints. For example: gorm:"column:email;type:varchar(255);unique;not null". The tag tells GORM how to map the struct field to the database column, including the SQL type for auto-migration and constraint enforcement.

Is my SQL data sent to a server?

No. All parsing and code generation runs entirely in your browser. Your SQL schema is never transmitted over the network, and no external services are contacted during the conversion process.

Real-world Examples

Go REST API with GORM models

A Go developer building a Gin-based REST API pastes their MySQL schema into this tool, generates GORM model structs with both GORM and JSON tags, and uses them as both database models and API response types. The TableName() method ensures GORM maps to the correct table names.

Input
CREATE TABLE subscriptions (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  plan VARCHAR(20) NOT NULL,
  amount DECIMAL(10,2) NOT NULL,
  active BOOLEAN DEFAULT true,
  expires_at TIMESTAMP,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Output
type Subscription struct {
    Id        int64          `json:"id" gorm:"column:id;primaryKey;autoIncrement;type:bigint"`
    UserId    int64          `json:"user_id" gorm:"column:user_id;type:bigint;not null"`
    Plan      string         `json:"plan" gorm:"column:plan;type:varchar(20);not null"`
    Amount    float64        `json:"amount" gorm:"column:amount;type:decimal(10,2);not null"`
    Active    *bool          `json:"active" gorm:"column:active;type:boolean;default:true"`
    ExpiresAt *time.Time     `json:"expires_at" gorm:"column:expires_at;type:timestamp"`
}

Migrating from raw SQL to GORM

A team with an existing PostgreSQL database migrates to GORM for better maintainability. They use this converter to generate model structs from their schema, then gradually replace raw SQL queries with GORM method chains while keeping the existing database unchanged.

Related Tools