Search…
Column Constraints
A TableIndex can be created directly from a Blueprint or from a existing Column. The TableIndex includes methods for further configuring the index which is required when defining foreign keys.

references

Set the referencing column for a foreign key relationship. For example, id for a country_id column.
Argument
Type
Required
Default
Description
columns
any
true
A column or array of columns that represents the foreign key reference.
Example:
SchemaBuilder
1
schema.create( "users", function( table ) {
2
table.unsignedInteger( "country_id" ).references( "id" ).onTable( "countries" );
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 NO ACTION
4
)
Copied!

onTable

Sets the referencing table for a foreign key relationship. For example, countries for a country_id column.
Argument
Type
Required
Default
Description
table
string
true
The referencing table name.
Example:
SchemaBuilder
1
schema.create( "users", function( table ) {
2
table.unsignedInteger( "country_id" ).references( "id" ).onTable( "countries" );
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 NO ACTION
4
)
Copied!

onUpdate

Set the strategy for updating foreign keys when the parent key is updated.
Argument
Type
Required
Default
Description
option
string
true
The strategy to use. Available values are: RESTRICT, CASCADE, SET NULL, NO ACTION, SET DEFAULT
Example:
SchemaBuilder
1
schema.create( "users", function( table ) {
2
table.unsignedInteger( "country_id" )
3
.references( "id" )
4
.onTable( "countries" )
5
.onUpdate( "CASCADE" );
6
} );
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 CASCADE ON DELETE NO ACTION
4
)
Copied!

onDelete

Set the strategy for updating foreign keys when the parent key is deleted.
Argument
Type
Required
Default
Description
option
string
true
The strategy to use. Available values are: RESTRICT, CASCADE, SET NULL, NO ACTION, SET DEFAULT
Example:
SchemaBuilder
1
schema.create( "users", function( table ) {
2
table.unsignedInteger( "country_id" )
3
.references( "id" )
4
.onTable( "countries" )
5
.onDelete( "SET NULL" );
6
} );
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 SET NULL
4
)
Copied!
Last modified 2yr ago
Copy link
Edit on GitHub