Skip to Content
fusion-orm
Models

Naming strategy

How Go model and field names become table, column, join-table, foreign-key, and index names — and how to override them.

Every derived identifier flows through a Namer (schema/naming.go:25-34). The implementation used by default is schema.NamingStrategy, which derives pluralized snake_case database names, gorm-style.

// Namer maps Go identifiers to database identifiers. NamingStrategy is the
// default implementation; each Tables cache is bound to one Namer.
type Namer interface {
	TableName(string) string
	ColumnName(table, column string) string
	JoinTableName(string) string
	FKName(table, column string) string
	IndexName(table, column string) string
	UniqueName(table, column string) string
}

NamingStrategy fields

// NamingStrategy derives pluralized snake_case database names, gorm-style.
type NamingStrategy struct {
	TablePrefix         string
	SingularTable       bool
	NameReplacer        Replacer
	NoLowerCase         bool
	IdentifierMaxLength int
}
FieldEffect
TablePrefixPrepended to every table and join-table name.
SingularTableSkips pluralization.
NameReplacerA Replacer (any type with Replace(string) string) that rewrites model identifiers before naming — the hook for acronyms or company conventions.
NoLowerCaseReturns identifiers as written, without snake_casing.
IdentifierMaxLengthTruncation limit for constraint/index names; the zero value means 64. Long names are cut and suffixed with an 8-hex SHA-1 fragment (schema/naming.go:86-97).

Set it through Config.NamingStrategy at Open time (fusion.go:73-74); the zero value is the default strategy (fusion.go:181-182):

db := openTestDB(t, &Config{
	NamingStrategy: schema.NamingStrategy{SingularTable: true, TablePrefix: "app_"},
	StrictTags:     true,
})

The default conversion

toDBName first rewrites common initialisms so they don't shatter into fragments (schema/naming.go:99-104): API, CPU, HTTP, ID, JSON, URL, UUID, XML, and 26 more are normalized to title case before camel-case splitting (schema/naming.go:121-150). Then each uppercase run becomes _lowercase. Tables are pluralized with jinzhu/inflection unless SingularTable is set (schema/naming.go:48-53).

Before/after pairs, pinned by schema/naming_test.go:10-31:

Go nameDefault output
Usertable users, column user
Persontable people (irregular plural)
Categorytable categories
UserIDcolumn user_id, table user_ids
HTTPServercolumn http_server
IDcolumn id, table ids
URLValuecolumn url_value
CreatedAtcolumn created_at

With SingularTable: true, TablePrefix: "app_", User becomes app_user (schema/naming_test.go:33-35); with NoLowerCase: true, UserID stays UserID (schema/naming_test.go:36-38).

Overriding a single model

The Tabler interface beats the strategy outright — no prefix, no pluralization (schema/table.go:204-210):

func (exampleUser) TableName() string { return "example_users" }

Join tables

JoinTableName names the junction table for many-to-many relations (schema/naming.go:61-69). An already-lowercase name passes through with just the prefix; a CamelCase name is snake_cased and pluralized:

{"JoinTableName lowercase", ns.JoinTableName("user_post"), "user_post"},
{"JoinTableName camel", ns.JoinTableName("UserPost"), "user_posts"},

AutoMigrate then creates that table as a two-column, composite-primary-key table so a duplicate pair cannot be inserted (migrator/migrator.go:514-524).

Foreign-key, index, and unique names

Constraints and indexes are named fk_/idx_/uni_<table>_<column> (schema/naming.go:72-84), all capped at IdentifierMaxLength (default 64) with a hash suffix when longer:

{"FKName", ns.FKName("users", "account_id"), "fk_users_account_id"},
{"IndexName", ns.IndexName("users", "email"), "idx_users_email"},
{"UniqueName", ns.UniqueName("users", "email"), "uni_users_email"},

These generated names are already table-scoped, so CREATE INDEX uses them unchanged. An explicit tag value (fusion:"index:idx_custom") shares the database-wide index namespace, so the table name is appended in the emitted DDL: two tables can declare the same index name (schema/field.go:79-88). Details in the tag reference.

Names are frozen at parse

Each Tables cache is bound to one Namer at Open time (schema/table.go:80-85). Changing naming conventions means reopening the database — a per-DB strategy change would invalidate the schema cache.

Next

On this page