Skip to content

Command Line Tools

dbmigrate create table

Generate a migration file for creating a new database table.

Terminal window
wheels dbmigrate create table name=<table_name> [--force] [--id] primaryKey=<key_name>

This command supports multiple parameter formats:

  • Named parameters: name=value (e.g., name=users, primaryKey=userId)
  • Flag parameters: --flag equals flag=true (e.g., --force equals force=true)
  • Flag with value: --flag=value equals flag=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

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.

ParameterTypeRequiredDefaultDescription
namestringYes-The name of the table to create
--forcebooleanNofalseForce the creation of the table
--idbooleanNotrueAuto create ID column as autoincrement ID
primaryKeystringNo”id”Overrides the default primary key column name

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.

Terminal window
# Named parameter (required)
wheels dbmigrate create table name=users
Terminal window
# Named + flag (recommended)
wheels dbmigrate create table name=user_roles --id=false
# OR all named
wheels db create table name=user_roles id=false
Terminal window
# Named parameters (recommended)
wheels dbmigrate create table name=products primaryKey=productCode
Terminal window
# Named + flag (recommended)
wheels dbmigrate create table name=users --force
# OR all named
wheels db create table name=users force=true

For the command:

Terminal window
wheels dbmigrate create table name=users

Generates 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");
}
}
}

Create a typical entity table:

Terminal window
# Generate the migration
wheels dbmigrate create table name=customer
# Then edit the migration file to add columns

Create a join table without primary key:

Terminal window
wheels dbmigrate create table name=products_categories --id=false

Create a table with non-standard primary key:

Terminal window
wheels dbmigrate create table name=legacy_customer primaryKey=customer_code

Wheels conventions expect singular table names:

Terminal window
# Good
wheels dbmigrate create table name=user
wheels dbmigrate create table name=product
# Avoid
wheels dbmigrate create table name=users
wheels dbmigrate create table name=products

After generating the migration, edit it to add columns:

// In the generated migration file
t = 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();

Think through your table structure before creating:

  • Primary key strategy
  • Required columns and their types
  • Foreign key relationships
  • Indexes needed for performance

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 up to run the generated migration