Skip to content

Migrator

addReference()

addReference() — returns void

Available in: migration Category: Migration Functions

Add a foreign key constraint to the database, using the reference name that was used to create it Only available in a migration CFC

NameTypeRequiredDefaultDescription
tablestringyes—The table name to perform the operation on
referenceNamestringno—The reference table name to perform the operation on
columnNamestringno—Alias for referenceName (consistent with the modern migrator surface — columnName / columnNames are accepted alongside the legacy form).
columnNamesstringno—Plural alias for referenceName. When both columnName and columnNames are supplied, columnNames wins.
// 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");
// }