Tag reference
Every fusion tag key, its gorm aliases, and its exact parse-time semantics.
A fusion:"..." tag is parsed once per field into a schema.Tag: an
optional bare column name plus key/value options. The parser lives in
schema/tag.go; both , and ; separate options, so gorm tags migrate
unchanged.
Parsing rules
- Keys are normalized to lowercase with spaces removed
(
schema/tag.go:38-40):primaryKey,Primary Key, andprimarykeyall becomeprimarykey, then alias topk. - Values are case-preserving and passed through verbatim.
- Quoted values survive commas:
type:"decimal(10,2)"parses as one option with valuedecimal(10,2)(schema/tag.go:86-88). ;maps 1:1 onto,viagormTagAliases(schema/tag.go:54-70):primarykey→pk,uniqueindex→unique,embedded→embed,autoincrement→autoincrement, and so on. The gormsize:Nalias maps totypewith the value passed through, but prefer an explicittype:varchar(N).- The first unknown token without a value becomes the bare column
name; subsequent unknown keys warn (through the logger) unless
Config.StrictTagsis set, in which case parse fails withschema.ErrInvalidTag(schema/tag.go:95-107,fusion.go:75-76).
The full key table
Every key in knownKeys (schema/tag.go:42-51), with its meaning:
| Tag | Meaning |
|---|---|
| (bare name) | Sets the column name when it appears as the first valueless token. Email string \fusion:"user_email"`→ columnuser_email`. |
column:NAME | Explicit column name; overrides the bare name when non-empty (schema/table.go:249-251). |
type:SQL | Overrides the inferred SQL type verbatim in DDL. type:uuid is special-cased: native uuid on PostgreSQL, TEXT on SQLite (migrator/migrator.go:151-158). |
pk (alias primarykey) | Marks the field a primary key. Repeat on several fields for a composite key — see composite primary keys. |
autoincrement (alias autoIncrement) | Database-assigned auto-increment key; the value is backfilled after insert and excluded from the INSERT column list (schema/table.go:195-198). On PostgreSQL this renders the PK column as BIGSERIAL (migrator/migrator.go:164-166). |
notnull | Adds NOT NULL to the column definition. |
nullzero | Writes NULL instead of the Go zero value on insert (schema/field.go:110). |
default:VALUE | Adds DEFAULT VALUE to the column. Literal values pass through as-is; expressions (now(), CURRENT_TIMESTAMP) are written verbatim too. Adding a column with a non-constant default to an existing SQLite table forces a full table rebuild — PostgreSQL adds it in place (migrator/migrator.go:317-331). |
index[:NAME] | Creates a non-unique index. Without a name it is derived as idx_<table>_<column>; with an explicit name, CREATE INDEX uses <NAME>_<table> so two tables can share an index name (schema/field.go:83-88). |
unique[:NAME] (alias uniqueindex) | Same as index but CREATE UNIQUE INDEX; derived name is uni_<table>_<column> (schema/naming.go:82-84). |
-> | Read-only: the field is not creatable and not updatable, but is readable — it appears in SELECTs only (schema/field.go:237-239). |
<- | Write-only: the field is creatable but not updatable and not readable — it never appears in a SELECT (schema/field.go:237-239). |
- | Skip the field entirely — no column is created (schema/table.go:222-224). |
embed[:PREFIX] (alias embedded) | Flattens a (named) embedded struct; PREFIX is prepended to every child column name. Anonymous structs embed without the tag. The value of embed is the prefix — there is no separate embeddedPrefix key. |
rel:KIND | Declares a relation: has-one, has-many, belongs-to, or many2many (schema/relation.go:16-19). See relations. |
foreignKey:FIELD | The foreign-key field for the relation, on the child side for has-one/has-many and on the owner side for belongs-to. |
references:FIELD | The referenced field on the other table; required when the target has a composite primary key. |
many2many:TABLE | Names the junction table for a many-to-many relation. AutoMigrate creates it as a two-column, composite-primary-key table (migrator/migrator.go:514-524). |
polymorphic[:VALUE] | Polymorphic relation: the foreign key resolves to <VALUE>_id / <VALUE>_type columns. The tag's VALUE (or, when empty, the snake_case model name — schema/relation.go:74-80) is the discriminator stored in the _type column. |
polymorphicValue:VALUE | Accepted as a known key but currently unused by relation resolution — set the discriminator via polymorphic:VALUE instead. |
serializer:json (or bare json) | Marshals the field through encoding/json; the field's data type becomes JSON (schema/field.go:208-211). See field types & serializers. |
constraint:SPEC | Foreign-key actions, split on , or ` |
softdelete | Marks a soft-delete field even if it is not named DeletedAt; the column type defaults to TIMESTAMP (schema/field.go:212-216). |
zerovalue:V | The value that means "row is live" for a non-NULL soft-delete field, e.g. zerovalue:0 (soft_delete.go:91-93). |
deletevalue:V | The value written to the soft-delete column on delete, e.g. deletevalue:1 (soft_delete.go:103-107). |
size:N (gorm alias) | Maps to type with the value passed through (schema/tag.go:69). Prefer type:varchar(N). |
NULL vs zero
nullzero affects the value written on insert; a soft-delete field's
"live" predicate is controlled by zerovalue/deletevalue or, for
timestamp fields, deleted_at IS NULL. They are different mechanisms —
see soft delete.
Golden examples
The parser's own test cases (schema/tag_test.go:15-29) are the shortest
honest demonstration of every rule above:
{"bare column", "email", "email", nil},
{"full", "user_email,type:varchar(255),pk,autoincrement,notnull,default:now(),index,unique", "user_email", map[string]string{
"type": "varchar(255)", "pk": "", "autoincrement": "", "notnull": "",
"default": "now()", "index": "", "unique": "",
}},
{"gorm semicolon migration", "column:user_email;primaryKey;autoIncrement;not null;uniqueIndex;foreignKey:UserID", "", map[string]string{
"column": "user_email", "pk": "", "autoincrement": "", "notnull": "",
"unique": "", "foreignkey": "UserID",
}},
{"quoted value", `type:"decimal(10,2)"`, "", map[string]string{"type": "decimal(10,2)"}},
{"relation", "rel:has-many,foreignKey:UserID,many2many:user_groups,polymorphic:owner", "", map[string]string{
"rel": "has-many", "foreignkey": "UserID", "many2many": "user_groups", "polymorphic": "owner",
}},Note the gorm-semicolon case: primaryKey;autoIncrement;not null;uniqueIndex
parses into pk / autoincrement / notnull / unique — spaces in keys
are stripped, and the aliases land on the fusion keys.