create-table Command

Create new database tables using the Akron CLI with customizable schemas across SQLite, MySQL, PostgreSQL, and MongoDB.

Overview

The create-table command allows you to create new database tables with custom schemas directly from the command line. It supports all Akron-compatible databases and provides a unified interface for table creation across different database systems.

Basic Syntax

akron create-table <table_name> --db <database_url> --schema <schema_json>

Parameters

table_name

Type: str (required)

Name of the table to create. Must be a valid identifier for the target database.

--db

Type: str (required)

Database connection URL. Supports sqlite://, mysql://, postgres://, and mongodb:// schemes.

--schema

Type: JSON string (required)

Table schema definition as JSON. Keys are column names, values are data types.

Examples

SQLite

1# Create users table in SQLite file
2akron create-table users --db "sqlite:///./myapp.db" --schema '{"id": "int", "name": "str", "email": "str"}'
3
4# Create in-memory SQLite table
5akron create-table sessions --db "sqlite:///:memory:" --schema '{"session_id": "str", "user_id": "int", "expires": "datetime"}'
Expected Output
✓ Table 'users' created successfully in SQLite database
✓ Columns: id (int), name (str), email (str)

MySQL

1# Create products table in MySQL
2akron create-table products --db "mysql://user:password@localhost:3306/store" --schema '{"id": "int", "name": "str", "price": "float", "in_stock": "bool"}'
3
4# Create with custom charset
5akron create-table articles --db "mysql://user:pass@host:3306/blog?charset=utf8mb4" --schema '{"id": "int", "title": "str", "content": "text"}'
Expected Output
✓ Table 'products' created successfully in MySQL database
✓ Columns: id (int), name (varchar), price (float), in_stock (boolean)

PostgreSQL

1# Create orders table in PostgreSQL
2akron create-table orders --db "postgres://user:password@localhost:5432/ecommerce" --schema '{"id": "int", "customer_id": "int", "total": "decimal", "created_at": "timestamp"}'
3
4# Create with SSL connection
5akron create-table logs --db "postgres://user:pass@host:5432/app?sslmode=require" --schema '{"id": "int", "level": "str", "message": "text", "timestamp": "timestamp"}'
Expected Output
✓ Table 'orders' created successfully in PostgreSQL database
✓ Columns: id (integer), customer_id (integer), total (decimal), created_at (timestamp)

MongoDB

1# Create collection in MongoDB (document schema)
2akron create-table users --db "mongodb://localhost:27017/myapp" --schema '{"_id": "objectid", "username": "str", "profile": "object", "tags": "array"}'
3
4# Create with authentication
5akron create-table events --db "mongodb://user:password@localhost:27017/analytics" --schema '{"event_type": "str", "data": "object", "timestamp": "datetime"}'
Expected Output
✓ Collection 'users' created successfully in MongoDB database
✓ Schema validation enabled for: _id (objectid), username (str), profile (object), tags (array)

Supported Data Types

Common Types

  • "int" - Integer numbers
  • "str" - String/text data
  • "bool" - Boolean true/false
  • "float" - Floating point numbers
  • "datetime" - Date and time
  • "date" - Date only
  • "time" - Time only

Advanced Types

  • "text" - Large text fields
  • "decimal" - Precise decimal numbers
  • "json" - JSON data (where supported)
  • "blob" - Binary data
  • "array" - Array data (MongoDB)
  • "object" - Nested objects (MongoDB)
  • "objectid" - MongoDB ObjectId

Complex Schema Examples

E-commerce Product Table

1akron create-table products --db "sqlite:///store.db" --schema '{
2 "id": "int",
3 "sku": "str",
4 "name": "str",
5 "description": "text",
6 "price": "decimal",
7 "cost": "decimal",
8 "category_id": "int",
9 "in_stock": "bool",
10 "stock_quantity": "int",
11 "created_at": "datetime",
12 "updated_at": "datetime"
13}'

User Profile with MongoDB

1akron create-table user_profiles --db "mongodb://localhost:27017/social" --schema '{
2 "_id": "objectid",
3 "username": "str",
4 "email": "str",
5 "profile": "object",
6 "preferences": "object",
7 "friends": "array",
8 "posts": "array",
9 "created_at": "datetime",
10 "last_login": "datetime"
11}'

Error Handling

Common Errors

  • Table already exists: Use drop-table first or choose a different name
  • Invalid schema format: Ensure JSON is properly formatted
  • Unsupported data type: Check database-specific type support
  • Connection failed: Verify database URL and credentials

Troubleshooting

1# Check if table exists first
2akron inspect-schema --db "sqlite:///myapp.db"
3
4# Drop existing table if needed
5akron drop-table users --db "sqlite:///myapp.db"
6
7# Then create new table
8akron create-table users --db "sqlite:///myapp.db" --schema '{"id": "int", "name": "str"}'

Related Commands