Migrator
addForeignKey()
Signature
Section titled “Signature”addForeignKey() — returns void
Available in: migration
Category: Migration Functions
Description
Section titled “Description”Add a foreign key constraint to the database, using the reference name that was used to create it Only available in a migration CFC
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
table | string | yes | — | The table name to perform the operation on |
referenceTable | string | yes | — | The reference table name to perform the operation on |
column | string | no | — | The column name to perform the operation on |
columnName | string | no | — | Modern alias for column (consistent with the rest of the migrator surface). |
referenceColumn | string | yes | — | The reference column name to perform the operation on |
Examples
Section titled “Examples”// 1. Add a foreign key from orders.customerId to customers.id
addForeignKey(
table="orders",
referenceTable="customers",
column="customerId",
referenceColumn="id"
);
// 2. Add a foreign key from comments.postId to posts.id
addForeignKey(
table="comments",
referenceTable="posts",
column="postId",
referenceColumn="id"
);
// 3. Use addForeignKey in a migration's up() and remove it in down()
// In your migration CFC:
//
// public void function up() {
// addForeignKey(
// table="order_items",
// referenceTable="orders",
// column="orderId",
// referenceColumn="id"
// );
// }
//
// public void function down() {
// dropForeignKey(table="order_items", keyName="FK_order_items_orders");
// }