Skip to content

Command Line Tools

dbmigrate create column

Generate a migration file for adding columns to an existing database table.

Terminal window
wheels dbmigrate create column name=<column_name> tableName=<table> dataType=<type> [options]

This command supports multiple parameter formats:

  • Named parameters: name=value (e.g., name=email, tableName=users, dataType=string)
  • Flag parameters: --flag equals flag=true (e.g., --allowNull=false)
  • Flag with value: --flag=value (same as named parameters)

Parameter Mixing Rules:

ALLOWED:

  • All named: name=email tableName=users dataType=string
  • Named parameters only (no positional support)

NOT ALLOWED:

  • Positional parameters: This command does not support positional parameters

Recommendation: Use named parameters for all values: name=email tableName=users dataType=string allowNull=false

The dbmigrate create column command generates a migration file that adds a column to an existing database table. It supports standard column types and various options for column configuration.

Note: Parameters are listed in the order they appear in the command signature.

ParameterTypeRequiredDefaultDescription
namestringYes-The column name to add
tableNamestringYes-The name of the database table to modify
dataTypestringYes-The column type to add
defaultanyNo-The default value to set for the column
allowNullbooleanNotrueShould the column allow nulls
limitnumberNo-The character limit of the column
precisionnumberNo-The precision of the numeric column
scalenumberNo-The scale of the numeric column
  • string - VARCHAR(255)
  • text - TEXT/CLOB
  • integer - INTEGER
  • biginteger - BIGINT
  • float - FLOAT
  • decimal - DECIMAL
  • boolean - BOOLEAN/BIT
  • date - DATE
  • time - TIME
  • datetime - DATETIME/TIMESTAMP
  • timestamp - TIMESTAMP
  • binary - BLOB/BINARY

The generated migration file will be named with a timestamp and description:

[timestamp]_[tablename]_[columnname]_create_column.cfc

Example:

20240125160000_user_email_create_column.cfc
Terminal window
wheels dbmigrate create column name=email tableName=user dataType=string
Terminal window
wheels dbmigrate create column name=is_active tableName=user dataType=boolean default=true
Terminal window
wheels dbmigrate create column name=bio tableName=user dataType=string allowNull=true limit=500
Terminal window
wheels dbmigrate create column name=price tableName=product dataType=decimal precision=10 scale=2

For the command:

Terminal window
wheels dbmigrate create column name=phone tableName=user dataType=string allowNull=true

Generates:

component extends="wheels.migrator.Migration" hint="create column phone in user table" {
function up() {
transaction {
addColumn(table="user", columnType="string", columnName="phone", allowNull=true);
}
}
function down() {
transaction {
removeColumn(table="user", column="phone");
}
}
}

Add preference column to user table:

Terminal window
# Create separate migrations for each column
wheels dbmigrate create column name=newsletter_subscribed tableName=user dataType=boolean default=true
wheels dbmigrate create column name=theme_preference tableName=user dataType=string default="light"

Add tracking column to any table:

Terminal window
wheels dbmigrate create column name=last_modified_by tableName=product dataType=integer allowNull=true
wheels dbmigrate create column name=last_modified_at tableName=product dataType=datetime allowNull=true

Add decimal columns for pricing:

Terminal window
wheels dbmigrate create column name=price tableName=product dataType=decimal precision=10 scale=2 default=0
wheels dbmigrate create column name=cost tableName=product dataType=decimal precision=10 scale=2

For existing tables with data, make new columns nullable or provide defaults:

Terminal window
# Good - nullable
wheels dbmigrate create column name=bio tableName=user dataType=text allowNull=true
# Good - with default
wheels dbmigrate create column name=status tableName=user dataType=string default="active"
# Bad - will fail if table has data (not nullable, no default)
wheels dbmigrate create column name=required_field tableName=user dataType=string --allowNull=false

Choose the right column type for your data:

Terminal window
# For short text
wheels dbmigrate create column name=username tableName=user dataType=string limit=50
# For long text
wheels dbmigrate create column name=content tableName=post dataType=text
# For money
wheels dbmigrate create column name=amount tableName=invoice dataType=decimal precision=10 scale=2

This command creates one column at a time:

Terminal window
# Create separate migrations for related columns
wheels dbmigrate create column name=address_line1 tableName=customer dataType=string
wheels dbmigrate create column name=city tableName=customer dataType=string
wheels dbmigrate create column name=state tableName=customer dataType=string limit=2

Think through column requirements before creating:

  • Data type and size
  • Null constraints
  • Default values
  • Index requirements

Add foreign key columns with appropriate types:

Terminal window
# Add foreign key column
wheels dbmigrate create column name=customer_id tableName=order dataType=integer
# Then create index in separate migration
wheels dbmigrate create blank name=add_order_customer_id_index

For special column types, use blank migrations:

Terminal window
# Create blank migration for custom column types
wheels dbmigrate create blank name=add_user_preferences_json
# Then manually add the column with custom SQL
Terminal window
# This will fail if table has data
wheels dbmigrate create column name=required_field tableName=user dataType=string --allowNull=false
# Do this instead
wheels dbmigrate create column name=required_field tableName=user dataType=string default="pending"

This command adds columns, not modifies them:

Terminal window
# Wrong - trying to change existing column type
wheels dbmigrate create column name=age tableName=user dataType=integer
# Right - use blank migration for modifications
wheels dbmigrate create blank name=change_user_age_to_integer
  • The migration includes automatic rollback with removeColumn()
  • Column order in down() is reversed for proper rollback
  • Always test migrations with data in development
  • Consider the impact on existing queries and code