Go Models Import

Import Go struct definitions to create database tables.

  • Endpoint: POST /studio/api/import/models (multipart file upload)
  • Upload a .go file containing struct definitions
  • The parser extracts struct fields, GORM tags, and type information
  • Creates corresponding database tables using CREATE TABLE statements
  • Relationship fields (slices, pointers to structs) are automatically skipped
  • Only available when ReadOnly is false

Example Input

Upload a .go file:

package models
 
type Category struct {
    ID   uint   `gorm:"primaryKey"`
    Name string `gorm:"size:100;not null"`
    Slug string `gorm:"size:100;uniqueIndex"`
}
 
type Product struct {
    ID         uint    `gorm:"primaryKey"`
    Name       string  `gorm:"size:200;not null"`
    Price      float64 `gorm:"type:decimal(10,2)"`
    CategoryID uint
    Category   Category // Relationship field - skipped
}

How It Works

  1. Parses Go structs using regex-based extraction
  2. Reads field names, Go types, and GORM struct tags
  3. Maps Go types to SQL types based on the database driver
  4. Skips relationship fields (slices, struct pointers, embedded structs)
  5. Generates and executes CREATE TABLE statements

Go Type to SQL Type Mapping

Go TypeSQL Type
uint, int, int64BIGINT
int32INTEGER
stringVARCHAR(255)
boolBOOLEAN
float32, float64DOUBLE
time.TimeTIMESTAMP
[]byteBLOB

GORM tags override default mappings. For example, gorm:"type:decimal(10,2)" uses DECIMAL(10,2) instead of DOUBLE.

Usage

curl -X POST http://localhost:8080/studio/api/import/models \
  -F "file=@models.go"

Response

{
  "tables_created": ["categories", "products"]
}