SQL to Ent Schema Generator

Convert SQL CREATE TABLE statements to Go Ent schema definitions (entgo.io/ent) with proper field types, SchemaType mappings, and annotations.

What is SQL to Ent Schema Generator?

Ent (entgo.io/ent) is a Go entity framework developed by Meta that uses code generation to produce type-safe database clients, builders, and CRUD helpers from schema definitions. Unlike traditional Go ORMs that rely on struct tags, Ent defines schemas as Go code with fluent field builders, giving you compile-time validation and auto-completion. This converter takes your SQL CREATE TABLE statements and generates Ent schema structs with Fields(), Edges(), and Annotations() methods, including SchemaType mappings that ensure the correct SQL column types for your chosen database dialect.

How to Use

  1. Set the Go package name for the generated schema files (default: "schema")
  2. Choose the target database dialect — MySQL, PostgreSQL, or SQLite — to get correct SchemaType mappings
  3. Toggle "Generate Edges stub" to scaffold edge definitions for entity relationships
  4. Paste your SQL CREATE TABLE statements into the input area
  5. Click "Generate Ent Schema" and copy the Go code into your Ent schema directory
  6. Run go generate ./ent to produce the full Ent client code from the generated schemas

Why Use This Tool?

Automatically maps SQL column types to Ent field types with SchemaType annotations for dialect-specific column definitions
Nullable columns receive both .Optional() and .Nillable() modifiers, matching Ent conventions for nullable Go fields
Column comments from SQL COMMENT syntax are preserved as .Comment() annotations for in-code documentation
Unique constraints and default values are translated to Ent field modifiers like .Unique() and .Default()
The entsql.Annotation ensures Ent maps schema structs to the correct SQL table names even when Go naming differs

Tips & Best Practices

  • SchemaType mapping is critical — without it, Ent would use generic SQL types that may not match your existing database schema
  • After generating schemas, run go generate ./ent to create the client, then use go test ./ent to verify the generated code compiles
  • Nullable fields use both .Optional() and .Nillable() — Optional allows omission during creation, Nillable permits nil pointer values in Go
  • Use the Edges stub option as a starting point for defining relationships, then customize with edge.From and edge.To in each schema

Frequently Asked Questions

How are SQL types mapped to Ent field types?

SQL types are mapped to Ent field types based on their semantic meaning: VARCHAR/CHAR/TEXT become field.String(), INT/INTEGER become field.Int(), BIGINT becomes field.Int64(), SMALLINT becomes field.Int16(), TINYINT becomes field.Int8(), DECIMAL/NUMERIC/FLOAT/DOUBLE become field.Float(), BOOLEAN becomes field.Bool(), DATETIME/TIMESTAMP/DATE become field.Time(), JSON becomes field.JSON(), BLOB/BINARY become field.Bytes(). Each field also gets a SchemaType annotation for dialect-specific SQL type mapping.

When should I avoid using Ent?

Ent is best suited for applications that want a fully code-generated, type-safe ORM. Avoid it if you prefer lightweight query builders like sqlx or squirrel, if you need to write raw SQL frequently, or if the code generation step does not fit your development workflow. For simpler Go projects, GORM or sqlx may be more pragmatic.

Why does the generated code use SchemaType?

Ent uses generic Go types (field.Int, field.String) by default, but databases have specific type names (bigint, varchar(255)). SchemaType tells Ent which SQL type to use when creating or migrating the database schema, ensuring the generated DDL matches your original table definition exactly.

What is the Annotations method for?

The Annotations() method adds table-level metadata. The entsql.Annotation with the Table field ensures Ent maps the schema struct to the correct SQL table name, which is especially important when the Go struct name (PascalCase) differs from the SQL table name (snake_case).

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

Microservice with Ent schema generation

A Go microservice team migrates from hand-written SQL to Ent. They paste their existing MySQL schema into this tool, generate Ent schema definitions with SchemaType annotations, and run go generate to produce a type-safe client. The generated client provides autocompletion for all queries in their IDE.

Input
CREATE TABLE accounts (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  email VARCHAR(255) UNIQUE NOT NULL,
  plan VARCHAR(20) DEFAULT 'free',
  storage_bytes BIGINT DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Output
func (Account) Fields() []ent.Field {
  return []ent.Field{
    field.Int64("id").Positive().SchemaType(map[string]string{dialect.MySql: "bigint"}),
    field.String("email").SchemaType(map[string]string{dialect.MySql: "varchar(255)"}).Unique(),
    field.String("plan").SchemaType(map[string]string{dialect.MySql: "varchar(20)"}).Default("free"),
  }
}

Multi-dialect schema with edges

A project that supports both PostgreSQL and SQLite uses the dialect selector to generate SchemaType mappings for each backend, then enables the Edges stub to define relationships between users, teams, and memberships tables.

Related Tools