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
tablestringyesThe Name of the table where the column is
columnNamestringyesTHe name of the column
columnTypestringyesThe type of the column
afterColumnstringnoThe name of the column which this column should be inserted after
referenceNamestringnoName for 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
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');