Search…
Creating Table Constraints
Sometimes you want to add constraints on a table level, rather than a column level. The following methods will let you accomplish that.

index

Create a generic index from one or more columns.
Argument
Type
Required
Default
Description
columns
string or array
true
The column or array of columns that make up the index.
name
string
false
A generated name consisting of the table name and column name(s).
The name of the index constraint.
Example:
SchemaBuilder
1
schema.create( "users", function( table ) {
2
table.string( "first_name" );
3
table.string( "last_name" );
4
table.index( [ "first_name", "last_name" ], "idx_users_full_name" );
5
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `users` (
2
`first_name` VARCHAR(255) NOT NULL,
3
`last_name` VARCHAR(255) NOT NULL,
4
INDEX `idx_users_full_name` (`first_name`, `last_name`)
5
)
Copied!

foreignKey

Create a foreign key constraint from one or more columns. Follow up this call with calls to the TableIndex's references and onTable methods.
Argument
Type
Required
Default
Description
columns
string or array
true
The column or array of columns that references a key or keys on another table.
name
string
false
A generated name consisting of the table name and column name(s).
The name of the foreign key constraint.
Example:
SchemaBuilder
1
schema.create( "users", function( table ) {
2
table.unsignedInteger( "country_id" );
3
table.foreignKey( "country_id" ).references( "id" ).onTable( "countries" );
4
} );
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 NO ACTION
4
)
Copied!

primaryKey

Create a primary key constraint from one or more columns.
Argument
Type
Required
Default
Description
columns
string or array
true
The column or array of columns that make up the primary key.
name
string
false
A generated name consisting of the table name and column name(s).
The name of the primary key constraint.
Example:
SchemaBuilder
1
schema.create( "posts_users", function( table ) {
2
table.unsignedInteger( "post_id" ).references( "id" ).onTable( "posts" );
3
table.unsignedInteger( "user_id" ).references( "id" ).onTable( "users" );
4
table.primaryKey( [ "post_id", "user_id" ], "pk_posts_users" );
5
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `posts_users` (
2
`post_id` VARCHAR(255) NOT NULL,
3
`user_id` VARCHAR(255) NOT NULL,
4
INDEX `idx_users_full_name` (`first_name`, `last_name`),
5
CONSTRAINT `fk_posts_users_post_id` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
6
CONSTRAINT `fk_posts_users_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
7
CONSTRAINT ""pk_users_first_name_last_name"" PRIMARY KEY (""first_name"", ""last_name"")
8
)
Copied!

unique

Create a unique constraint from one or more columns.
Argument
Type
Required
Default
Description
columns
string or array
true
The column or array of columns that make up the unique constraint.
name
string
false
A generated name consisting of the table name and column name(s).
The name of the unique constraint.
Example:
SchemaBuilder
1
schema.create( "users", function( table ) {
2
table.increments( "id" );
3
table.string( "username ");
4
table.unique( "username" );
5
} );
Copied!
SQL (MySQL)
1
CREATE TABLE `users` (
2
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
3
UNIQUE (`username`)
4
)
Copied!
Last modified 2yr ago
Copy link
Edit on GitHub