Skip to Content
fusion-orm
Models

Field types & serializers

How Go field types map to SQL types on each dialect, byte/time/JSON handling, and the Serializer interface.

Every field's Go type is reduced to a broad DataType during parse (schema/field.go:165-198), and the migrator turns that data type into a concrete SQL column type per dialect (migrator/migrator.go:111-147).

Data types

const (
	DataTypeBool      DataType = "bool"
	DataTypeInt       DataType = "int"
	DataTypeUint      DataType = "uint"
	DataTypeFloat     DataType = "float"
	DataTypeString    DataType = "string"
	DataTypeBytes     DataType = "bytes"
	DataTypeTime      DataType = "time"
	DataTypeJSON      DataType = "json"
	DataTypeInterface DataType = "interface"
)

The inference rules (schema/field.go:165-198):

  • time.Time → time.
  • Anything implementing sql.Scanner (or *T implementing it) → string — this is how uuid.UUID and DeletedAt behave.
  • bool → bool; all signed widths → int; all unsigned widths → uint; float32/float64 → float; string → string.
  • []byte → bytes; any other slice, map, or struct → json.
  • Everything else (interfaces, unknown kinds) → interface.

Go type to SQL type per dialect

The mapping lives in the migrator's sqlType, keyed on the dialect's PostgreSQLTypes feature flag — PostgreSQL declares it, SQLite does not (migrator/migrator.go:115-146):

Go typeDataTypePostgreSQLSQLite
boolboolBOOLEANINTEGER
any signed/unsigned intint/uintBIGINTINTEGER
float32/float64floatDOUBLE PRECISIONREAL
stringstringTEXTTEXT
[]bytebytesBYTEABLOB
map / non-byte slice / structjsonJSONBTEXT
time.TimetimeTIMESTAMPTIMESTAMP
interface / unknowninterfaceTEXTTEXT

An autoincrement primary key becomes BIGSERIAL on PostgreSQL (the SerialColumn flag) and INTEGER PRIMARY KEY AUTOINCREMENT on SQLite (migrator/migrator.go:164-185).

An explicit type: tag overrides the mapping verbatim in DDL — including type:uuid, which the migrator special-cases: native uuid on PostgreSQL, TEXT on SQLite (migrator/migrator.go:151-158). This is the mechanism behind fusion.UUIDModel.

All ints are wide

There is no per-width mapping: int8 and int64 both become BIGINT on PostgreSQL and INTEGER on SQLite. Add type: for tighter columns.

Byte slices

[]byte fields are rendered as hex literals: SQLite's X'...' (dialect/sqlite/sqlite.go:72-84) or PostgreSQL's '\x...' (dialect/base.go:98-114); a nil slice renders NULL. With the default mapping they are stored as BLOB (SQLite) or BYTEA (PostgreSQL).

time.Time

Timestamps are appended as UTC literals in 2006-01-02 15:04:05.999999-07:00 format (dialect/base.go:117-122), and stored as TIMESTAMP on both dialects. The soft-delete field follows the same storage regardless of its Go type — it is forced to the time data type and a TIMESTAMP SQL type when the tag does not set one (schema/field.go:212-216, 241-243).

JSON serialization

Two spellings enable the built-in JSON serializer — serializer:json and the bare json key (schema/field.go:208-211):

var ser Serializer
if tag.Has("json") || tag.Get("serializer") == "json" {
	ser = JSONSerializer{}
}

The serializer marshals through encoding/json and forces the field's data type to json (schema/field.go:247-249) — so maps, non-byte slices, structs, and even string/[]byte fields tagged json store a JSON document (JSONB on PostgreSQL, TEXT on SQLite).

type metaModel struct {
	ID        int64  `fusion:"pk,autoincrement"`
	Email     string `fusion:"user_email,notnull,index"`
	Nick      string `fusion:"unique,nickname"`
	Blob      []byte `fusion:"json"`
	Custom    string `fusion:"serializer:json,type:varchar(9),default:'x'"`
	CreatedAt time.Time
	UpdatedAt time.Time
	RO        string `fusion:"->"`
	WO        string `fusion:"<-"`
}

The parsed metadata confirms the round trip for this model (schema/field_test.go:103-107): Blob and Custom both carry the json data type — Custom additionally keeps its explicit varchar(9) SQL type.

Custom serializers

Anything satisfying the schema.Serializer interface can customize how a field becomes a SQL value:

// Serializer customizes how a field is stored as a JSON/SQL value.
type Serializer interface {
	Serialize(reflect.Value) ([]byte, error)
	Deserialize([]byte, reflect.Value) error
}
// JSONSerializer marshals a field through encoding/json.
type JSONSerializer struct{}

JSONSerializer is the only implementation in-tree. The Serializer interface is the extension point — a serializer is resolved per field at parse time and invoked by the compiled appender (schema/appender.go:22-29) — but there is no config-level hook to register a custom one yet; you would assign schema.Field.Serializer after parse.

A complete round trip

A settings column stored as JSON, written and read back:

type Account struct {
	ID       int64          `fusion:"pk,autoincrement"`
	Name     string         `fusion:"notnull"`
	Settings map[string]any `fusion:"json"`
}

func (Account) TableName() string { return "accounts" }

user := Account{Name: "Ada", Settings: map[string]any{"tier": "pro"}}
db.Create(&user) // INSERT renders '{"tier":"pro"}' as a JSON literal

var got Account
db.First(&got, user.ID) // Settings scans back through encoding/json
fmt.Println(got.Settings["tier"]) // pro

The write renders through the dialect's JSON appender — a quoted string literal with quote-doubling and NUL protection (dialect/base.go:128-155).

Next

On this page