SQL to Knex Migration Generator

Convert SQL CREATE TABLE statements to Knex migration files with up and down functions for PostgreSQL, MySQL, and SQLite.

Output:

What is SQL to Knex Migration Generator?

Knex.js is a battle-tested SQL query builder for Node.js that provides a fluent API for building queries, a migration system for schema evolution, and connection pooling for production workloads. Unlike ORMs, Knex stays close to SQL while adding JavaScript convenience and composability. This converter takes your SQL CREATE TABLE statements and generates Knex migration files with both up (create tables) and down (drop tables) functions, using Knex schema builder methods that map SQL types to the appropriate JavaScript API calls.

How to Use

  1. Select your database dialect — PostgreSQL, MySQL, or SQLite — since Knex generates different SQL per dialect
  2. Choose output language: TypeScript (with Knex type imports) or JavaScript (with CommonJS exports)
  3. Paste your SQL CREATE TABLE statements into the input area
  4. Click "Generate Knex Migration" to produce the migration file with up and down functions
  5. Copy the output and save it in your Knex migrations directory with a timestamp prefix

Why Use This Tool?

Generates Knex schema builder calls that map SQL types to the correct Knex methods — SERIAL to increments(), VARCHAR to string(), TIMESTAMP to timestamp()
Handles PRIMARY KEY, NOT NULL, DEFAULT, UNIQUE, and REFERENCES constraints as chained Knex method calls
Produces both up and down migration functions — down drops tables in reverse order to respect foreign key dependencies
Supports TypeScript output with proper Knex type imports for type-safe migration files
DEFAULT NOW() and CURRENT_TIMESTAMP are converted to knex.fn.now() for cross-database compatibility

Tips & Best Practices

  • SERIAL columns map to table.increments() which automatically creates a primary key — no need to chain .primary()
  • Foreign key references generate .references('column').inTable('table') chains that Knex uses for constraint creation
  • The down migration drops tables in reverse creation order, preventing foreign key constraint errors during rollback
  • Use TypeScript output for better IDE autocompletion and type checking when writing additional migration logic

Frequently Asked Questions

How are SQL types mapped to Knex schema builder methods?

SQL types map to Knex methods: SERIAL/BIGSERIAL map to increments()/bigIncrements(), INTEGER maps to integer(), BIGINT maps to bigInteger(), VARCHAR maps to string(length), TEXT maps to text(), BOOLEAN maps to boolean(), TIMESTAMP maps to timestamp(), DATE maps to date(), UUID maps to uuid(), DECIMAL maps to decimal(precision, scale), JSONB maps to jsonb(), BLOB maps to binary(). Each method returns a chainable column builder.

When should I avoid using Knex?

Knex is a query builder, not an ORM — it does not provide models, associations, or change tracking. Avoid it if you need an ORM with active records (consider Objection.js or TypeORM instead), if you want auto-generated migrations from schema diffs (consider Prisma), or if you need a fully type-safe query API (consider Kysely).

What is the difference between up and down migrations?

The up migration creates tables, indexes, and constraints using knex.schema.createTable(). The down migration reverses those changes using knex.schema.dropTableIfExists(). Down migrations drop tables in reverse order to handle foreign key dependencies correctly during rollback.

Does the converter handle foreign key constraints?

Yes. REFERENCES constraints are converted to .references('column').inTable('table') chains on the column builder. Knex uses these to create foreign key constraints in the database. You can add .onDelete() and .onUpdate() cascade rules manually after generation.

Is my SQL data sent to a server?

No. All parsing and code generation runs entirely in your browser. Your SQL schema is never transmitted over the network, and no external services are contacted during the conversion process.

Real-world Examples

Node.js API with Knex migrations

A Node.js developer building an Express API pastes their PostgreSQL schema into this tool, generates a Knex migration file, and runs knex migrate:latest to create the database tables. The generated up/down functions enable reliable schema versioning and rollback.

Input
CREATE TABLE orders (
  id SERIAL PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES users(id),
  total DECIMAL(10,2) NOT NULL,
  status VARCHAR(20) DEFAULT 'pending',
  created_at TIMESTAMP DEFAULT NOW()
);
Output
await knex.schema.createTable('orders', (table) => {
  table.increments('id').primary();
  table.integer('customer_id').notNullable().references('id').inTable('users');
  table.decimal('total', 10, 2).notNullable();
  table.string('status', 20).defaultTo('pending');
  table.timestamp('created_at').defaultTo(knex.fn.now());
});

Multi-database project with TypeScript migrations

A project that supports both PostgreSQL and SQLite uses the dialect selector to generate different migration files, choosing TypeScript output for type safety. The team maintains separate migration directories for each database backend.

Related Tools