Skip to main content

Fields

Field expressions provide type-safe access to model fields. Use them in queries to ensure compile-time type checking.

Complete reference for all field types and options.

Field Types​

Numeric Fields​

Int64​

64-bit integer:

schema.Int64("id").Primary().AutoIncrement().Build()
schema.Int64("age").Build()

Int32​

32-bit integer:

schema.Int32("count").Build()

Float64​

64-bit floating point:

schema.Float64("price").Build()

Decimal​

Decimal with precision:

schema.Decimal("amount").
MaxDigits(10).
DecimalPlaces(2).
Build()

String Fields​

String​

Variable-length string:

schema.String("username").
MaxLength(150).
Required().
Build()

Text​

Unlimited text:

schema.Text("content").Build()

Email​

Email field with validation:

schema.Email("email").
Unique().
Required().
Build()

URL​

URL field with validation:

schema.URL("website").Build()

Slug​

URL-friendly string:

schema.Slug("slug").
MaxLength(200).
Unique().
Build()

Boolean Fields​

Bool​

Boolean field:

schema.Bool("is_active").
Default(true).
Build()

Date and Time Fields​

Time​

Timestamp:

schema.Time("created_at").
AutoNowAdd().
Build()

Date​

Date only:

schema.Date("birth_date").Build()

DateTime​

Date and time:

schema.DateTime("last_login").Build()

Special Fields​

UUID​

UUID field:

schema.UUID("id").Primary().Build()

JSON​

JSON field:

schema.JSON("metadata").Build()

Bytes​

Binary data:

schema.Bytes("avatar").Build()

Field Options​

All field types support these options:

Required​

Make field required (NOT NULL):

schema.String("username").Required().Build()

Unique​

Add unique constraint:

schema.String("email").Unique().Build()

Primary​

Set as primary key:

schema.Int64("id").Primary().Build()

Index​

Create database index:

schema.String("username").Index().Build()

DBColumn​

Custom column name:

schema.String("firstName").DBColumn("first_name").Build()

Default​

Default value:

schema.Bool("is_active").Default(true).Build()
schema.String("status").Default("pending").Build()

MaxLength​

Maximum length (strings):

schema.String("username").MaxLength(150).Build()

MinLength​

Minimum length (strings):

schema.String("password").MinLength(8).Build()

MaxDigits​

Maximum digits (decimal):

schema.Decimal("amount").
MaxDigits(10).
DecimalPlaces(2).
Build()

DecimalPlaces​

Decimal places (decimal):

schema.Decimal("price").
MaxDigits(10).
DecimalPlaces(2).
Build()

Choices​

Predefined choices:

schema.String("status").
Choices("active", "pending", "deleted").
Build()

Null​

Allow NULL:

schema.String("middle_name").Null().Build()

Blank​

Allow blank (strings):

schema.String("description").Blank().Build()

HelpText​

Help text for admin:

schema.String("username").
HelpText("Required. 150 characters or fewer.").
Build()

VerboseName​

Human-readable name:

schema.String("firstName").
VerboseName("First Name").
Build()

Editable​

Make read-only in admin:

schema.Time("created_at").
Editable(false).
Build()

AutoNow​

Set on save:

schema.Time("updated_at").AutoNow().Build()

AutoNowAdd​

Set on create only:

schema.Time("created_at").AutoNowAdd().Build()

AutoIncrement​

Auto-incrementing (primary keys):

schema.Int64("id").
Primary().
AutoIncrement().
Build()

Field Type Reference Table​

Field TypeGo TypeSQL TypeBuilder
Int64int64BIGINTschema.Int64()
Int32int32INTEGERschema.Int32()
StringstringVARCHAR(n)schema.String()
TextstringTEXTschema.Text()
BoolboolBOOLEANschema.Bool()
Float64float64DOUBLE PRECISIONschema.Float64()
Decimaldecimal.DecimalNUMERICschema.Decimal()
Timetime.TimeTIMESTAMPschema.Time()
Datetime.TimeDATEschema.Date()
DateTimetime.TimeTIMESTAMPschema.DateTime()
EmailstringVARCHAR(255)schema.Email()
URLstringVARCHAR(255)schema.URL()
UUIDstringUUIDschema.UUID()
JSON[]byteJSONBschema.JSON()
Bytes[]byteBYTEAschema.Bytes()

See Also​