TableBuilder

Classes > Migration > TableBuilder

Builder for creating table definitions with columns, indexes, foreign keys, and constraints.

public sealed class TableBuilder

Example

// Define a table with various column types and constraints
.Table("employees", t => t
    .Column("id", PortableTypes.Uuid, c => c.PrimaryKey().DefaultLql("gen_uuid()"))
    .Column("name", PortableTypes.VarChar(100), c => c.NotNull())
    .Column("email", PortableTypes.VarChar(255), c => c.NotNull())
    .Column("department_id", PortableTypes.Int)
    .Column("salary", PortableTypes.Decimal(12, 2))
    .Column("hired_at", PortableTypes.DateTime, c => c.DefaultLql("now()"))
    .Index("idx_employees_email", "email", unique: true)
    .Index("idx_employees_dept", "department_id")
    .ForeignKey("department_id", "departments", "id"))

Methods

Column(string, PortableType, Action<ColumnBuilder>?)

public TableBuilder Column(string name, PortableType type, Action<ColumnBuilder>? configure = null)

Add a column to the table.

Parameters:

Name Type Description
name String
type PortableType
configure ColumnBuilder>

Returns: TableBuilder

Index(string, string, bool, string?)

public TableBuilder Index(string name, string column, bool unique = false, string? filter = null)

Add an index to the table.

Parameters:

Name Type Description
name String
column String
unique Boolean
filter String

Returns: TableBuilder

Index(string, string[], bool, string?)

public TableBuilder Index(string name, string[] columns, bool unique = false, string? filter = null)

Add a multi-column index to the table.

Parameters:

Name Type Description
name String
columns String[]
unique Boolean
filter String

Returns: TableBuilder

ExpressionIndex(string, string, bool, string?)

public TableBuilder ExpressionIndex(string name, string expression, bool unique = false, string? filter = null)

Add an expression-based index (e.g., lower(name) for case-insensitive matching). Expressions are emitted verbatim in the CREATE INDEX statement.

Parameters:

Name Type Description
name String
expression String
unique Boolean
filter String

Returns: TableBuilder

ExpressionIndex(string, string[], bool, string?)

public TableBuilder ExpressionIndex(string name, string[] expressions, bool unique = false, string? filter = null)

Add a multi-expression index (e.g., lower(name), suburb_id for composite expression indexes). Expressions are emitted verbatim in the CREATE INDEX statement.

Parameters:

Name Type Description
name String
expressions String[]
unique Boolean
filter String

Returns: TableBuilder

ForeignKey(string, string, string, ForeignKeyAction, ForeignKeyAction)

public TableBuilder ForeignKey(string column, string referencedTable, string referencedColumn, ForeignKeyAction onDelete = ForeignKeyAction.NoAction, ForeignKeyAction onUpdate = ForeignKeyAction.NoAction)

Add a foreign key to the table.

Parameters:

Name Type Description
column String
referencedTable String
referencedColumn String
onDelete ForeignKeyAction
onUpdate ForeignKeyAction

Returns: TableBuilder

CompositePrimaryKey(params string[])

public TableBuilder CompositePrimaryKey(params string[] columns)

Define a composite primary key spanning multiple columns.

Parameters:

Name Type Description
columns String[]

Returns: TableBuilder

Unique(string, params string[])

public TableBuilder Unique(string name, params string[] columns)

Add a unique constraint to the table.

Parameters:

Name Type Description
name String
columns String[]

Returns: TableBuilder

Check(string, string)

public TableBuilder Check(string name, string expression)

Add a check constraint to the table.

Parameters:

Name Type Description
name String
expression String

Returns: TableBuilder

Comment(string)

public TableBuilder Comment(string comment)

Add a comment to the table.

Parameters:

Name Type Description
comment String

Returns: TableBuilder