Table Builder

The table builder defines columns within a CreateTable or AlterTable callback. Each method returns the table for chaining.

Column types

MethodDescription
Increments("id")Auto-increment primary key
BigIncrements("id")Bigint auto-increment
String("name", 255)VARCHAR(size)
Text("body")TEXT
Boolean("active")BOOLEAN
Integer("count")INTEGER
BigInteger("big_id")BIGINT
Float("price", 8, 2)FLOAT(precision, scale)
Decimal("amount", 10, 2)DECIMAL
Timestamp("published_at")DATETIME
Timestamps()created_at, updated_at
SoftDeletes()deleted_at (nullable)
ForeignId("user_id", "users.id")Foreign key column

Modifiers

Chain Nullable() and Default() after a column:

t.String("status", 50).Default("'draft'")
t.Text("bio").Nullable()
t.Integer("views").Default("0")

Example

schema.New(db).CreateTable("users", func(t *schema.Table) {
    t.Increments("id")
    t.String("name", 255)
    t.String("email", 255)
    t.Text("password")
    t.Boolean("active").Default("1")
    t.Timestamps()
    t.SoftDeletes()
})