Command Line Tools
dbmigrate create table
dbmigrate create table
Section titled “dbmigrate create table”Generate a migration file for creating a new database table.
Synopsis
Section titled “Synopsis”wheels dbmigrate create table name=<table_name> [--force] [--id] primaryKey=<key_name>CommandBox Parameter Syntax
Section titled “CommandBox Parameter Syntax”This command supports multiple parameter formats:
- Named parameters:
name=value(e.g.,name=users,primaryKey=userId) - Flag parameters:
--flagequalsflag=true(e.g.,--forceequalsforce=true) - Flag with value:
--flag=valueequalsflag=value(e.g.,--id=false)
Parameter Mixing Rules:
ALLOWED:
- All named:
name=users primaryKey=userId - Named + flags:
name=users --force --id=false
NOT ALLOWED:
- Positional parameters: This command does not support positional parameters
Recommendation: Use named for required parameters, flags for booleans: name=users --force
Description
Section titled “Description”The dbmigrate create table command generates a migration file that creates a new database table. The generated migration includes the table structure following Wheels conventions.
Parameters
Section titled “Parameters”| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | Yes | - | The name of the table to create |
--force | boolean | No | false | Force the creation of the table |
--id | boolean | No | true | Auto create ID column as autoincrement ID |
primaryKey | string | No | ”id” | Overrides the default primary key column name |
Notes About Column Definition
Section titled “Notes About Column Definition”The generated migration file will contain a basic table structure. You’ll need to manually edit the migration file to add columns with their types and options. The migration template includes comments showing how to add columns.
Examples
Section titled “Examples”Create a basic table
Section titled “Create a basic table”# Named parameter (required)wheels dbmigrate create table name=usersCreate table without ID column
Section titled “Create table without ID column”# Named + flag (recommended)wheels dbmigrate create table name=user_roles --id=false
# OR all namedwheels db create table name=user_roles id=falseCreate table with custom primary key
Section titled “Create table with custom primary key”# Named parameters (recommended)wheels dbmigrate create table name=products primaryKey=productCodeForce creation (overwrite existing)
Section titled “Force creation (overwrite existing)”# Named + flag (recommended)wheels dbmigrate create table name=users --force
# OR all namedwheels db create table name=users force=trueGenerated Migration Example
Section titled “Generated Migration Example”For the command:
wheels dbmigrate create table name=usersGenerates a migration file that you can customize:
component extends="wheels.migrator.Migration" hint="create users table" {
function up() { transaction { t = createTable(name="users", force=false, id=true, primaryKey="id"); // Add your columns here // t.string(columnNames="name"); // t.integer(columnNames="age"); t.timestamps(); t.create(); } }
function down() { transaction { dropTable("users"); } }
}Use Cases
Section titled “Use Cases”Standard Entity Table
Section titled “Standard Entity Table”Create a typical entity table:
# Generate the migrationwheels dbmigrate create table name=customer
# Then edit the migration file to add columnsJoin Table for Many-to-Many
Section titled “Join Table for Many-to-Many”Create a join table without primary key:
wheels dbmigrate create table name=products_categories --id=falseTable with Custom Primary Key
Section titled “Table with Custom Primary Key”Create a table with non-standard primary key:
wheels dbmigrate create table name=legacy_customer primaryKey=customer_codeBest Practices
Section titled “Best Practices”1. Use Singular Table Names
Section titled “1. Use Singular Table Names”Wheels conventions expect singular table names:
# Goodwheels dbmigrate create table name=userwheels dbmigrate create table name=product
# Avoidwheels dbmigrate create table name=userswheels dbmigrate create table name=products2. Edit Migration Files
Section titled “2. Edit Migration Files”After generating the migration, edit it to add columns:
// In the generated migration filet = createTable(name="orders", force=false, id=true, primaryKey="id");t.integer(columnNames="customer_id");t.decimal(columnNames="total", precision=10, scale=2);t.string(columnNames="status", default="pending");t.timestamps();t.create();3. Plan Your Schema
Section titled “3. Plan Your Schema”Think through your table structure before creating:
- Primary key strategy
- Required columns and their types
- Foreign key relationships
- Indexes needed for performance
Working with the Generated Migration
Section titled “Working with the Generated Migration”The command generates a basic migration template. You’ll need to edit it to add columns:
component extends="wheels.migrator.Migration" { function up() { transaction { t = createTable(name="tableName", force=false, id=true, primaryKey="id"); // Add your columns here: t.string(columnName="name"); t.integer(columnName="age"); t.boolean(columnName="active", default=true); t.text(columnName="description"); // MySQL only: use size parameter for larger text fields t.text(columnName="content", size="mediumtext"); // 16MB t.text(columnName="largeContent", size="longtext"); // 4GB t.timestamps(); t.create(); } }
function down() { transaction { dropTable("tableName"); } }}- 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 upto run the generated migration
Related Commands
Section titled “Related Commands”wheels dbmigrate create column- Add columns to existing tablewheels dbmigrate create blank- Create custom migrationwheels dbmigrate remove table- Create table removal migrationwheels dbmigrate up- Run migrationswheels dbmigrate info- View migration status