Skip to content

Migrator

addIndex()

addIndex() — returns void

Available in: migration Category: Migration Functions

Add database index on a table column Only available in a migration CFC

NameTypeRequiredDefaultDescription
tablestringyes—The table name to perform the index operation on
columnNamesstringno—One or more column names to index, comma separated
uniquebooleannofalseIf true will create a unique index constraint
indexNamestringno—The name of the index to add: Defaults to table name + underscore + first column name
// 1. Add a basic index on a single column
addIndex(table="users", columnNames="email");

// 2. Add a unique index to enforce uniqueness on a column
addIndex(table="members", columnNames="username", unique=true);

// 3. Add a composite index on multiple columns
addIndex(table="orders", columnNames="customerId,createdAt");

// 4. Add an index with a custom index name
// (defaults to tableName_firstColumnName, e.g. "posts_publishedAt")
addIndex(table="posts", columnNames="publishedAt", indexName="idx_posts_published");