Migrator
addReference()
Signature
Section titled “Signature”addReference() — 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 |
referenceName | string | no | — | The reference table name to perform the operation on |
columnName | string | no | — | Alias for referenceName (consistent with the modern migrator surface — columnName / columnNames are accepted alongside the legacy form). |
columnNames | string | no | — | Plural alias for referenceName. When both columnName and columnNames are supplied, columnNames wins. |
Examples
Section titled “Examples”// 1. Add a foreign key from comments.postId to posts.id using a reference name
// Equivalent to: addForeignKey(table="comments", referenceTable="posts", column="postId", referenceColumn="id")
addReference(table="comments", referenceName="post");
// 2. Add a foreign key from order_items.orderId to orders.id
addReference(table="order_items", referenceName="order");
// 3. Use addReference in a migration's up() and undo it with dropReference() in down()
// In your migration CFC:
//
// public void function up() {
// addReference(table="comments", referenceName="post");
// }
//
// public void function down() {
// dropReference(table="comments", referenceName="post");
// }