LogoLogo
HomeAPIBlog
3.0.0-SNAPSHOT
3.0.0-SNAPSHOT
  • INTRODUCTION
    • Getting Started
      • Running Local Development Servers
      • Beginner Tutorial: Hello World
      • Beginner Tutorial: Hello Database
      • Tutorial: Wheels, AJAX, and You
    • Frameworks and Wheels
    • Requirements
    • Manual Installation
    • Upgrading
    • Screencasts
  • Command Line Tools
    • CLI Overview
    • Quick Start Guide
    • Command Reference
      • Core Commands
        • wheels init
        • wheels info
        • wheels reload
        • wheels deps
        • wheels destroy
        • wheels watch
      • Code Generation
        • wheels generate app
        • wheels generate app-wizard
        • wheels generate controller
        • wheels generate model
        • wheels generate view
        • wheels generate property
        • wheels generate route
        • wheels generate resource
        • wheels generate api-resource
        • wheels generate frontend
        • wheels generate test
        • wheels generate snippets
        • wheels scaffold
      • Database Commands
        • wheels dbmigrate info
        • wheels dbmigrate latest
        • wheels dbmigrate up
        • wheels dbmigrate down
        • wheels dbmigrate reset
        • wheels dbmigrate exec
        • wheels dbmigrate create blank
        • wheels dbmigrate create table
        • wheels dbmigrate create column
        • wheels dbmigrate remove table
        • wheels db schema
        • wheels db seed
      • Testing Commands
        • wheels test
        • wheels test run
        • wheels test coverage
        • wheels test debug
      • Configuration Commands
        • wheels config list
        • wheels config set
        • wheels config env
      • Environment Management
        • wheels env
        • wheels env setup
        • wheels env list
        • wheels env switch
      • Plugin Management
        • wheels plugins
        • wheels plugins list
        • wheels plugins install
        • wheels plugins remove
      • Code Analysis
        • wheels analyze
        • wheels analyze code
        • wheels analyze performance
        • wheels analyze security
      • Security Commands
        • wheels security
        • wheels security scan
      • Performance Commands
        • wheels optimize
        • wheels optimize performance
      • Documentation Commands
        • wheels docs
        • wheels docs generate
        • wheels docs serve
      • CI/CD Commands
        • wheels ci init
      • Docker Commands
        • wheels docker init
        • wheels docker deploy
      • Deployment Commands
        • wheels deploy
        • wheels deploy audit
        • wheels deploy exec
        • wheels deploy hooks
        • wheels deploy init
        • wheels deploy lock
        • wheels deploy logs
        • wheels deploy proxy
        • wheels deploy push
        • wheels deploy rollback
        • wheels deploy secrets
        • wheels deploy setup
        • wheels deploy status
        • wheels deploy stop
    • CLI Development Guides
      • Creating Commands
      • Service Architecture
      • Migrations Guide
      • Testing Guide
  • Working with Wheels
    • Conventions
    • Configuration and Defaults
    • Directory Structure
    • Switching Environments
    • Testing Your Application
    • Using the Test Environment
    • Contributing to Wheels
    • Submitting Pull Requests
    • Documenting your Code
  • Handling Requests with Controllers
    • Request Handling
    • Rendering Content
    • Redirecting Users
    • Sending Files
    • Sending Email
    • Responding with Multiple Formats
    • Using the Flash
    • Using Filters
    • Verification
    • Event Handlers
    • Routing
    • URL Rewriting
      • Apache
      • IIS
      • Tomcat
      • Nginx
    • Obfuscating URLs
    • Caching
    • Nesting Controllers
    • CORS Requests
  • Displaying Views to Users
    • Pages
    • Partials
    • Linking Pages
    • Layouts
    • Form Helpers and Showing Errors
    • Displaying Links for Pagination
    • Date, Media, and Text Helpers
    • Creating Custom View Helpers
    • Localization
  • Database Interaction Through Models
    • Object Relational Mapping
    • Creating Records
    • Reading Records
    • Updating Records
    • Deleting Records
    • Column Statistics
    • Dynamic Finders
    • Getting Paginated Data
    • Associations
    • Nested Properties
    • Object Validation
    • Object Callbacks
    • Calculated Properties
    • Transactions
    • Dirty Records
    • Soft Delete
    • Automatic Time Stamps
    • Database Migrations
      • Migrations in Production
    • Using Multiple Data Sources
  • Plugins
    • Installing and Using Plugins
    • Developing Plugins
    • Publishing Plugins
  • Project Documentation
    • Overview
  • External Links
    • Source Code
    • Issue Tracker
    • Sponsor Us
    • Community
Powered by GitBook
LogoLogo
On this page
  • Synopsis
  • Description
  • Arguments
  • <table_name>
  • <column_name>:<type>[:options]
  • Options
  • --datasource
  • --after
  • --force
  • Column Types
  • Column Options
  • Examples
  • Add a single column
  • Add multiple columns
  • Add column with positioning
  • Add columns with indexes
  • Generated Migration Example
  • Use Cases
  • Adding User Preferences
  • Adding Audit Fields
  • Adding Calculated Fields
  • Adding Search Columns
  • Best Practices
  • 1. Consider NULL Values
  • 2. Use Appropriate Types
  • 3. Plan for Indexes
  • 4. Group Related Changes
  • Advanced Scenarios
  • Adding Foreign Keys
  • Adding JSON Columns
  • Positional Columns
  • Common Pitfalls
  • 1. Non-Nullable Without Default
  • 2. Changing Column Types
  • Notes
  • Related Commands

Was this helpful?

Edit on GitHub
Export as PDF
  1. Command Line Tools
  2. Command Reference
  3. Database Commands

wheels dbmigrate create column

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

Synopsis

wheels dbmigrate create column <table_name> <column_name>:<type>[:options] [more_columns...] [options]

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>

  • Type: String

  • Required: Yes

  • Description: The name of the table to add columns to

<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

  • Type: String

  • Default: Application default

  • Description: Target datasource for the migration

--after

  • Type: String

  • Default: None

  • Description: Position new column(s) after specified column

--force

  • Type: Boolean

  • Default: false

  • Description: Overwrite existing migration file

Column Types

  • 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

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

wheels dbmigrate create column user email:string

Add multiple columns

wheels dbmigrate create column product sku:string:unique weight:decimal:precision=8:scale=2 is_featured:boolean:default=false

Add column with positioning

wheels dbmigrate create column user middle_name:string:null --after=first_name

Add columns with indexes

wheels dbmigrate create column order shipped_at:datetime:index tracking_number:string:index

Generated Migration Example

For the command:

wheels dbmigrate create column user phone:string:null country_code:string:limit=2:default='US'

Generates:

component extends="wheels.migrator.Migration" hint="Add columns to user table" {

    function up() {
        transaction {
            addColumn(table="user", column="phone", type="string", null=true);
            addColumn(table="user", column="country_code", type="string", limit=2, default="US");
        }
    }

    function down() {
        transaction {
            removeColumn(table="user", column="country_code");
            removeColumn(table="user", column="phone");
        }
    }

}

Use Cases

Adding User Preferences

Add preference columns to user table:

wheels dbmigrate create column user \
  newsletter_subscribed:boolean:default=true \
  notification_email:boolean:default=true \
  theme_preference:string:default='light'

Adding Audit Fields

Add tracking columns to any table:

wheels dbmigrate create column product \
  last_modified_by:integer:null \
  last_modified_at:datetime:null \
  version:integer:default=1

Adding Calculated Fields

Add columns for denormalized/cached data:

wheels dbmigrate create column order \
  item_count:integer:default=0 \
  subtotal:decimal:precision=10:scale=2:default=0 \
  tax_amount:decimal:precision=10:scale=2:default=0

Adding Search Columns

Add columns optimized for searching:

wheels dbmigrate create column article \
  search_text:text:null \
  slug:string:unique:index \
  tags:string:null

Best Practices

1. Consider NULL Values

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

# Good - nullable
wheels dbmigrate create column user bio:text:null

# Good - with default
wheels dbmigrate create column user status:string:default='active'

# Bad - will fail if table has data
wheels dbmigrate create column user required_field:string

2. Use Appropriate Types

Choose the right column type for your data:

# For short text
wheels dbmigrate create column user username:string:limit=50

# For long text
wheels dbmigrate create column post content:text

# For money
wheels dbmigrate create column invoice amount:decimal:precision=10:scale=2

3. Plan for Indexes

Add indexes for columns used in queries:

# Add indexed column
wheels dbmigrate create column order customer_email:string:index

# Or create separate index migration
wheels dbmigrate create blank --name=add_order_customer_email_index

4. Group Related Changes

Add related columns in a single migration:

# Add all address fields together
wheels dbmigrate create column customer \
  address_line1:string \
  address_line2:string:null \
  city:string \
  state:string:limit=2 \
  postal_code:string:limit=10 \
  country:string:limit=2:default='US'

Advanced Scenarios

Adding Foreign Keys

Add foreign key columns with appropriate types:

# Add foreign key column
wheels dbmigrate create column order customer_id:integer:index

# Then create constraint in blank migration
wheels dbmigrate create blank --name=add_order_customer_foreign_key

Adding JSON Columns

For databases that support JSON:

# Create blank migration for JSON column
wheels dbmigrate create blank --name=add_user_preferences_json
# Then manually add JSON column type

Positional Columns

Control column order in table:

# Add after specific column
wheels dbmigrate create column user display_name:string --after=username

Common Pitfalls

1. Non-Nullable Without Default

# This will fail if table has data
wheels dbmigrate create column user required_field:string

# Do this instead
wheels dbmigrate create column user required_field:string:default='TBD'

2. Changing Column Types

This command adds columns, not modifies them:

# Wrong - trying to change type
wheels dbmigrate create column user age:integer

# Right - use blank migration for modifications
wheels dbmigrate create blank --name=change_user_age_to_integer

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

Previouswheels dbmigrate create tableNextwheels dbmigrate remove table

Last updated 2 days ago

Was this helpful?

- Create new tables

- Create custom migrations

- Remove tables

- Run migrations

- Rollback migrations

wheels dbmigrate create table
wheels dbmigrate create blank
wheels dbmigrate remove table
wheels dbmigrate up
wheels dbmigrate down