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>
  • [columns...]
  • Options
  • --id
  • --no-id
  • --timestamps
  • --no-timestamps
  • --datasource
  • --force
  • Column Types
  • Column Options
  • Examples
  • Create a basic table
  • Create table with columns
  • Create table with column options
  • Create table without timestamps
  • Create join table without primary key
  • Generated Migration Example
  • Use Cases
  • Standard Entity Table
  • Join Table for Many-to-Many
  • Configuration Table
  • Audit Log Table
  • Best Practices
  • 1. Use Singular Table Names
  • 2. Include Foreign Keys
  • 3. Set Appropriate Defaults
  • 4. Consider Indexes
  • Advanced Options
  • Custom Primary Key
  • Composite Keys
  • 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 table

Generate a migration file for creating a new database table.

Synopsis

wheels dbmigrate create table <table_name> [columns...] [options]

Description

The dbmigrate create table command generates a migration file that creates a new database table with specified columns. It automatically includes timestamp columns (createdAt, updatedAt) and provides a complete table structure following CFWheels conventions.

Arguments

<table_name>

  • Type: String

  • Required: Yes

  • Description: The name of the table to create (singular form recommended)

[columns...]

  • Type: String (multiple)

  • Required: No

  • Format: name:type:options

  • Description: Column definitions in the format name:type:options

Options

--id

  • Type: String

  • Default: id

  • Description: Name of the primary key column (use --no-id to skip)

--no-id

  • Type: Boolean

  • Default: false

  • Description: Skip creating a primary key column

--timestamps

  • Type: Boolean

  • Default: true

  • Description: Include createdAt and updatedAt columns

--no-timestamps

  • Type: Boolean

  • Default: false

  • Description: Skip creating timestamp columns

--datasource

  • Type: String

  • Default: Application default

  • Description: Target datasource for the migration

--force

  • Type: Boolean

  • Default: false

  • Description: Overwrite existing migration file

Column Types

Supported 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

Column options are specified after the type with colons:

  • :null - Allow NULL values

  • :default=value - Set default value

  • :limit=n - Set column length/size

  • :precision=n - Set decimal precision

  • :scale=n - Set decimal scale

Examples

Create a basic table

wheels dbmigrate create table user

Create table with columns

wheels dbmigrate create table post title:string content:text author_id:integer published:boolean

Create table with column options

wheels dbmigrate create table product name:string:limit=100 price:decimal:precision=10:scale=2 description:text:null

Create table without timestamps

wheels dbmigrate create table configuration key:string value:text --no-timestamps

Create join table without primary key

wheels dbmigrate create table users_roles user_id:integer role_id:integer --no-id

Generated Migration Example

For the command:

wheels dbmigrate create table post title:string content:text author_id:integer published:boolean

Generates:

component extends="wheels.migrator.Migration" hint="Create post table" {

    function up() {
        transaction {
            createTable(name="post", force=false) {
                t.increments("id");
                t.string("title");
                t.text("content");
                t.integer("author_id");
                t.boolean("published");
                t.timestamps();
            }
        }
    }

    function down() {
        transaction {
            dropTable("post");
        }
    }

}

Use Cases

Standard Entity Table

Create a typical entity table:

wheels dbmigrate create table customer \
  first_name:string \
  last_name:string \
  email:string:limit=150 \
  phone:string:null \
  is_active:boolean:default=true

Join Table for Many-to-Many

Create a join table for relationships:

wheels dbmigrate create table products_categories \
  product_id:integer \
  category_id:integer \
  display_order:integer:default=0 \
  --no-id

Configuration Table

Create a settings/configuration table:

wheels dbmigrate create table setting \
  key:string:limit=50 \
  value:text \
  description:text:null \
  --no-timestamps

Audit Log Table

Create an audit trail table:

wheels dbmigrate create table audit_log \
  table_name:string \
  record_id:integer \
  action:string:limit=10 \
  user_id:integer \
  old_values:text:null \
  new_values:text:null \
  ip_address:string:limit=45

Best Practices

1. Use Singular Table Names

CFWheels conventions expect singular table names:

# Good
wheels dbmigrate create table user
wheels dbmigrate create table product

# Avoid
wheels dbmigrate create table users
wheels dbmigrate create table products

2. Include Foreign Keys

Add foreign key columns for relationships:

wheels dbmigrate create table order \
  customer_id:integer \
  total:decimal:precision=10:scale=2 \
  status:string:default='pending'

3. Set Appropriate Defaults

Provide sensible defaults where applicable:

wheels dbmigrate create table article \
  title:string \
  content:text \
  is_published:boolean:default=false \
  view_count:integer:default=0

4. Consider Indexes

Plan for indexes (add them in separate migrations):

# Create table
wheels dbmigrate create table user email:string username:string

# Create index migration
wheels dbmigrate create blank --name=add_user_indexes

Advanced Options

Custom Primary Key

Specify a custom primary key name:

wheels dbmigrate create table legacy_customer \
  customer_code:string \
  name:string \
  --id=customer_code

Composite Keys

For composite primary keys, use blank migration:

# First create without primary key
wheels dbmigrate create table order_item \
  order_id:integer \
  product_id:integer \
  quantity:integer \
  --no-id

# Then create blank migration for composite key
wheels dbmigrate create blank --name=add_order_item_composite_key

Notes

  • Table names should follow your database naming conventions

  • The migration automatically handles rollback with dropTable()

  • Column order in the command is preserved in the migration

  • Use wheels dbmigrate up to run the generated migration

Related Commands

Previouswheels dbmigrate create blankNextwheels dbmigrate create column

Last updated 2 days ago

Was this helpful?

- Add columns to existing table

- Create custom migration

- Create table removal migration

- Run migrations

- View migration status

wheels dbmigrate create column
wheels dbmigrate create blank
wheels dbmigrate remove table
wheels dbmigrate up
wheels dbmigrate info