Command Line Tools
dbmigrate create column
dbmigrate create column
Section titled “dbmigrate create column”Generate a migration file for adding columns to an existing database table.
Synopsis
Section titled “Synopsis”wheels dbmigrate create column name=<column_name> tableName=<table> dataType=<type> [options]CommandBox Parameter Syntax
Section titled “CommandBox Parameter Syntax”This command supports multiple parameter formats:
- Named parameters:
name=value(e.g.,name=email,tableName=users,dataType=string) - Flag parameters:
--flagequalsflag=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
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”Note: Parameters are listed in the order they appear in the command signature.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | Yes | - | The column name to add |
tableName | string | Yes | - | The name of the database table to modify |
dataType | string | Yes | - | The column type to add |
default | any | No | - | The default value to set for the column |
allowNull | boolean | No | true | Should the column allow nulls |
limit | number | No | - | The character limit of the column |
precision | number | No | - | The precision of the numeric column |
scale | number | No | - | The scale of the numeric column |
Column Types
Section titled “Column Types”string- VARCHAR(255)text- TEXT/CLOBinteger- INTEGERbiginteger- BIGINTfloat- FLOATdecimal- DECIMALboolean- BOOLEAN/BITdate- DATEtime- TIMEdatetime- DATETIME/TIMESTAMPtimestamp- TIMESTAMPbinary- BLOB/BINARY
Migration File Naming
Section titled “Migration File Naming”The generated migration file will be named with a timestamp and description:
[timestamp]_[tablename]_[columnname]_create_column.cfcExample:
20240125160000_user_email_create_column.cfcExamples
Section titled “Examples”Add a simple column
Section titled “Add a simple column”wheels dbmigrate create column name=email tableName=user dataType=stringAdd column with default value
Section titled “Add column with default value”wheels dbmigrate create column name=is_active tableName=user dataType=boolean default=trueAdd nullable column with limit
Section titled “Add nullable column with limit”wheels dbmigrate create column name=bio tableName=user dataType=string allowNull=true limit=500Add decimal column with precision
Section titled “Add decimal column with precision”wheels dbmigrate create column name=price tableName=product dataType=decimal precision=10 scale=2Generated Migration Example
Section titled “Generated Migration Example”For the command:
wheels dbmigrate create column name=phone tableName=user dataType=string allowNull=trueGenerates:
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"); } }
}Use Cases
Section titled “Use Cases”Adding User Preferences
Section titled “Adding User Preferences”Add preference column to user table:
# Create separate migrations for each columnwheels dbmigrate create column name=newsletter_subscribed tableName=user dataType=boolean default=truewheels dbmigrate create column name=theme_preference tableName=user dataType=string default="light"Adding Audit Fields
Section titled “Adding Audit Fields”Add tracking column to any table:
wheels dbmigrate create column name=last_modified_by tableName=product dataType=integer allowNull=truewheels dbmigrate create column name=last_modified_at tableName=product dataType=datetime allowNull=trueAdding Price Fields
Section titled “Adding Price Fields”Add decimal columns for pricing:
wheels dbmigrate create column name=price tableName=product dataType=decimal precision=10 scale=2 default=0wheels dbmigrate create column name=cost tableName=product dataType=decimal precision=10 scale=2Best Practices
Section titled “Best Practices”1. Consider NULL Values
Section titled “1. Consider NULL Values”For existing tables with data, make new columns nullable or provide defaults:
# Good - nullablewheels dbmigrate create column name=bio tableName=user dataType=text allowNull=true
# Good - with defaultwheels 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=false2. Use Appropriate Types
Section titled “2. Use Appropriate Types”Choose the right column type for your data:
# For short textwheels dbmigrate create column name=username tableName=user dataType=string limit=50
# For long textwheels dbmigrate create column name=content tableName=post dataType=text
# For moneywheels dbmigrate create column name=amount tableName=invoice dataType=decimal precision=10 scale=23. One Column Per Migration
Section titled “3. One Column Per Migration”This command creates one column at a time:
# Create separate migrations for related columnswheels dbmigrate create column name=address_line1 tableName=customer dataType=stringwheels dbmigrate create column name=city tableName=customer dataType=stringwheels dbmigrate create column name=state tableName=customer dataType=string limit=24. Plan Your Schema
Section titled “4. Plan Your Schema”Think through column requirements before creating:
- Data type and size
- Null constraints
- Default values
- Index requirements
Advanced Scenarios
Section titled “Advanced Scenarios”Adding Foreign Keys
Section titled “Adding Foreign Keys”Add foreign key columns with appropriate types:
# Add foreign key columnwheels dbmigrate create column name=customer_id tableName=order dataType=integer
# Then create index in separate migrationwheels dbmigrate create blank name=add_order_customer_id_indexComplex Column Types
Section titled “Complex Column Types”For special column types, use blank migrations:
# Create blank migration for custom column typeswheels dbmigrate create blank name=add_user_preferences_json# Then manually add the column with custom SQLCommon Pitfalls
Section titled “Common Pitfalls”1. Non-Nullable Without Default
Section titled “1. Non-Nullable Without Default”# This will fail if table has datawheels dbmigrate create column name=required_field tableName=user dataType=string --allowNull=false
# Do this insteadwheels dbmigrate create column name=required_field tableName=user dataType=string default="pending"2. Changing Column Types
Section titled “2. Changing Column Types”This command adds columns, not modifies them:
# Wrong - trying to change existing column typewheels dbmigrate create column name=age tableName=user dataType=integer
# Right - use blank migration for modificationswheels 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
Related Commands
Section titled “Related Commands”wheels dbmigrate create table- Create new tableswheels dbmigrate create blank- Create custom migrationswheels dbmigrate remove table- Remove tableswheels dbmigrate up- Run migrationswheels dbmigrate down- Rollback migrations