Column Modifiers
When creating a column from the Blueprint
object, a Column
object is returned. This column
gives you access to a few modifier commands to further configure the column.
comment
Attach a comment to the column.
Argument
Type
Required
Default
Description
comment
string
true
The comment text.
Example:
SchemaBuilder
SQL (MySQL)
default
Sets a default value for the column.
Note: The value is not escaped, allowing you to specify functions like NOW()
or literals like 1
. To specify a literal string, wrap the value in quotes.
Argument
Type
Required
Default
Description
value
string
true
The default value.
Example:
SchemaBuilder
SQL (MySQL)
nullable
Sets the column to allow null values.
Argument
Type
Required
Default
Description
No arguments
All columns are created as NOT NULL
by default. As such, there is no notNull
method.
Example:
SchemaBuilder
SQL (MySQL)
primaryKey
Adds the column as a primary key for the table.
Argument
Type
Required
Default
Description
indexName
string
false
A derived name built from the table name and column name.
The name to use for the primary key constraint.
The primaryKey
method returns a TableIndex
instance. Additional methods can be chained off of it.
Example:
SchemaBuilder
SQL (MySQL)
references
Creates a foreign key constraint for the column.
Argument
Type
Required
Default
Description
value
string
true
The default value.
IMPORTANT: Additional configuration of the foreign constraint is done by calling methods on the returned TableIndex
instance.
Example:
SchemaBuilder
SQL (MySQL)
unsigned
Sets the column as unsigned.
Argument
Type
Required
Default
Description
No arguments
Example:
SchemaBuilder
SQL (MySQL)
unique
Sets the column to have the UNIQUE constraint.
Argument
Type
Required
Default
Description
No arguments
Example:
SchemaBuilder
SQL (MySQL)
withCurrent
Sets the column to have the a default value of CURRENT_TIMESTAMP
.
Argument
Type
Required
Default
Description
No arguments
Example:
SchemaBuilder
SQL (Postgres)
storedAs
Creates a stored computed column. Computed columns are defined as expressions between other columns and/or constant values. Stored computed columns are saved in the database to avoid computing on every query.
Your database grammar may not differentiate between stored computed columns and virtual computed columns. Research your grammar's implementation for more details.
Argument
Type
Required
Default
Description
expression
string
true
The SQL used to define the computed column.
virtualAs
Creates a virtual computed column. Computed columns are defined as expressions between other columns and/or constant values. Virtual computed columns are computed on every query.
Your database grammar may not differentiate between stored computed columns and virtual computed columns. Research your grammar's implementation for more details.
Argument
Type
Required
Default
Description
expression
string
true
The SQL used to define the computed column.