Skip to content

Migrator

addColumn()

addColumn() — returns void

Available in: migration Category: Migration Functions

Adds a new column to an existing table. This function is only available inside a migration CFC and is part of the Wheels migrator API. Use it to evolve your database schema safely through versioned migrations.

NameTypeRequiredDefaultDescription
tablestringyesThe Name of the table to add the column to
columnTypestringyesThe type of the new column
columnNamestringyesTHe name of the new column
afterColumnstringnoThe name of the column which this column should be inserted after
referenceNamestringnoName for new reference column, see documentation for references function, required if columnType is ‘reference’
defaultstringnoDefault value for this column
allowNullbooleannoWhether to allow NULL values
limitnumericnoCharacter or integer size limit for column
precisionnumericnoprecision value for decimal columns, i.e. number of digits the column can hold
scalenumericnoscale value for decimal columns, i.e. number of digits that can be placed to the right of the decimal point (must be less than or equal to precision)
1. Add a simple string column
addColumn(
    table="members",
    columnType="string",
    columnName="status",
    limit=50
);

Adds a status column (string, max 50 chars) to the members table.

2. Add an integer column with default value
addColumn(
    table="orders",
    columnType="integer",
    columnName="priority",
    default=0
);

Adds a priority column with default value 0.

3. Add a boolean column that does not allow NULL
addColumn(
    table="users",
    columnType="boolean",
    columnName="isActive",
    allowNull=false,
    default=1
);

Adds an isActive column with default value true (1), disallowing NULL.

4. Add a decimal column with precision and scale
addColumn(
    table="products",
    columnType="decimal",
    columnName="price",
    precision=10,
    scale=2
);

Adds a price column with up to 10 digits total, including 2 decimal places.

5. Add a reference (foreign key) column
addColumn(
    table="orders",
    columnType="reference",
    columnName="userId",
    referenceName="users"
);

Adds a userId column to orders and links it to the users table.