Search…
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
1
schema.create( "users", function( table ) {
2
table.integer( "age" ).comment( "Do not lie about your age" );
3
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `users` (
2
`age` INTEGER NOT NULL COMMENT `Do not lie about your age`
3
)
Copied!

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
1
schema.create( "users", function( table ) {
2
table.boolean( "is_active" ).default( 1 );
3
table.timestamp( "created_date" ).default( "NOW()" );
4
tablVIRTUAL NOT NULLe.string( "country" ).default( "'USA'" );
5
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `users` (
2
`is_active` TINYINT(1) DEFAULT 1,
3
`created_date` TIMESTAMP DEFAULT NOW(),
4
`country` VARCHAR(255) DEFAULT 'USA'
5
)
Copied!

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
1
schema.create( "users", function( table ) {
2
table.timestamp( "last_logged_in" ).nullable()
3
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `users` (
2
`last_logged_in` TIMESTAMP
3
)
Copied!

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
1
schema.create( "users", function( table ) {
2
table.uuid( "id" ).primaryKey();
3
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `users` (
2
`id` CHAR(35) NOT NULL,
3
CONSTAINT `pk_users_id` PRIMARY KEY (`id`)
4
)
Copied!

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
1
schema.create( "users", function( table ) {
2
table.unsignedInteger( "country_id" ).references( "id" ).onTable( "countries" ).onDelete( "cascade" );
3
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `users` (
2
`country_id` INTEGER UNSIGNED NOT NULL,
3
CONSTRAINT `fk_users_country_id` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON UPDATE NO ACTION ON DELETE CASCADE
4
)
Copied!

unsigned

Sets the column as unsigned.
Argument
Type
Required
Default
Description
No arguments
Example:
SchemaBuilder
1
schema.create( "users", function( table ) {
2
table.integer( age" ).unsigned();
3
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `users` (
2
`age` INTEGER UNSIGNED NOT NULL
3
)
Copied!

unique

Sets the column to have the UNIQUE constraint.
Argument
Type
Required
Default
Description
No arguments
Example:
SchemaBuilder
1
schema.create( "email", function( table ) {
2
table.string( email" ).unique();
3
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `users` (
2
`email` VARCHAR(255) NOT NULL UNIQUE
3
)
Copied!

withCurrent

Sets the column to have the a default value of CURRENT_TIMESTAMP.
Argument
Type
Required
Default
Description
No arguments
Example:
SchemaBuilder
1
schema.create( "posts", function( table ) {
2
table.timestamp( "posted_date" ).withCurrent();
3
} );
Copied!
SQL (Postgres)
1
CREATE TABLE "posts" (
2
"posted_date" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
3
)
Copied!

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.
SchemaBuilder
1
schema.create( "products", function( table ) {
2
table.integer( "price" );
3
table.integer( "tax" ).storedAs( "price * 0.0675" );
4
} );
Copied!
MySQL
SQL Server
Postgres
Oracle
1
CREATE TABLE `products` (
2
`price` INTEGER NOT NULL,
3
`tax` INTEGER GENERATED ALWAYS AS (price * 0.0675) STORED NOT NULL
4
)
Copied!
1
CREATE TABLE [products] (
2
[price] INTEGER NOT NULL,
3
[tax] AS (price * 0.0675) PERSISTED
4
)
Copied!
1
CREATE TABLE "products" (
2
"price" INTEGER NOT NULL,
3
"tax" INTEGER NOT NULL GENERATED ALWAYS AS (price * 0.0675) STORED
4
)
Copied!
1
CREATE TABLE "PRODUCTS" (
2
"PRICE" NUMBER(10, 0) NOT NULL,
3
"TAX" NUMBER(10, 0) GENERATED ALWAYS AS (price * 0.0675)
4
)
Copied!

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.
SchemaBuilder
1
schema.create( "products", function( table ) {
2
table.integer( "price" );
3
table.integer( "tax" ).virtualAs( "price * 0.0675" );
4
} );
Copied!
MySQL
SQL Server
Postgres
Oracle
1
CREATE TABLE `products` (
2
`price` INTEGER NOT NULL,
3
`tax` INTEGER GENERATED ALWAYS AS (price * 0.0675) VIRTUAL NOT NULL
4
)
Copied!
1
CREATE TABLE [products] (
2
[price] INTEGER NOT NULL,
3
[tax] AS (price * 0.0675)
4
)
Copied!
1
CREATE TABLE "products" (
2
"price" INTEGER NOT NULL,
3
"tax" INTEGER GENERATED ALWAYS AS (price * 0.0675) STORED
4
)
Copied!
1
CREATE TABLE "PRODUCTS" (
2
"PRICE" NUMBER(10, 0) NOT NULL,
3
"TAX" NUMBER(10, 0) GENERATED ALWAYS AS (price * 0.0675) VIRTUAL
4
)
Copied!
Last modified 6mo ago