wheels dbmigrate create column
Generate a migration file for adding columns to an existing database table.
Synopsis
Description
The dbmigrate create column
command generates a migration file that adds one or more columns to an existing database table. It supports all standard column types and options, making it easy to evolve your database schema incrementally.
Arguments
<table_name>
<table_name>
Type: String
Required: Yes
Description: The name of the table to add columns to
<column_name>:<type>[:options]
<column_name>:<type>[:options]
Type: String
Required: Yes (at least one)
Format:
name:type:option1:option2=value
Description: Column definition(s) to add
Options
--datasource
--datasource
Type: String
Default: Application default
Description: Target datasource for the migration
--after
--after
Type: String
Default: None
Description: Position new column(s) after specified column
--force
--force
Type: Boolean
Default:
false
Description: Overwrite existing migration file
Column Types
string
- VARCHAR(255)text
- TEXT/CLOBinteger
- INTEGERbiginteger
- BIGINTfloat
- FLOATdecimal
- DECIMALboolean
- BOOLEAN/BITdate
- DATEtime
- TIMEdatetime
- DATETIME/TIMESTAMPtimestamp
- TIMESTAMPbinary
- BLOB/BINARY
Column Options
:null
- Allow NULL values:default=value
- Set default value:limit=n
- Set column length:precision=n
- Set decimal precision:scale=n
- Set decimal scale:index
- Create an index on this column:unique
- Add unique constraint
Examples
Add a single column
Add multiple columns
Add column with positioning
Add columns with indexes
Generated Migration Example
For the command:
Generates:
Use Cases
Adding User Preferences
Add preference columns to user table:
Adding Audit Fields
Add tracking columns to any table:
Adding Calculated Fields
Add columns for denormalized/cached data:
Adding Search Columns
Add columns optimized for searching:
Best Practices
1. Consider NULL Values
For existing tables with data, make new columns nullable or provide defaults:
2. Use Appropriate Types
Choose the right column type for your data:
3. Plan for Indexes
Add indexes for columns used in queries:
4. Group Related Changes
Add related columns in a single migration:
Advanced Scenarios
Adding Foreign Keys
Add foreign key columns with appropriate types:
Adding JSON Columns
For databases that support JSON:
Positional Columns
Control column order in table:
Common Pitfalls
1. Non-Nullable Without Default
2. Changing Column Types
This command adds columns, not modifies them:
Notes
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
Last updated
Was this helpful?