Skip to content

Migrator

changeColumn()

changeColumn() — returns void

Available in: migration Category: Migration Functions

Changes the definition of an existing column in a database table. This function is used in migration CFCs to update column properties such as type, size, default value, nullability, precision, and scale.

NameTypeRequiredDefaultDescription
tablestringyes—The Name of the table where the column is
columnNamestringyes—THe name of the column
columnTypestringyes—The type of the column
afterColumnstringno—The name of the column which this column should be inserted after
referenceNamestringno—Name for reference column, see documentation for references function, required if columnType is ‘reference’
defaultstringno—Default value for this column
allowNullbooleanno—Whether to allow NULL values
limitnumericno—Character or integer size limit for column
precisionnumericno—(For decimal type) the maximum number of digits allow
scalenumericno—(For decimal type) the number of digits to the right of the decimal point
addColumnsbooleannofalseif true, attempts to add columns and database will likely throw an error if column already exists
1. Change the type and limit of a column
changeColumn(table='members', columnName='status', columnType='string', limit=50);

2. Change a decimal column’s precision and scale
changeColumn(table='products', columnName='price', columnType='decimal', precision=10, scale=2);

3. Change a column to allow NULL and set a default value
changeColumn(table='users', columnName='nickname', columnType='string', limit=100, allowNull=true, default='Guest');

4. Move a column to a specific position in the table
changeColumn(table='orders', columnName='status', columnType='string', limit=20, afterColumn='orderDate');