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?

This tool converts SQL CREATE TABLE statements into Knex migration files. Knex.js is a SQL query builder for JavaScript that supports migrations, transactions, and connection pooling. The converter generates both up and down migration functions for PostgreSQL, MySQL, or SQLite.

How to Use

  1. Select your database dialect (PostgreSQL, MySQL, or SQLite)
  2. Choose output language: TypeScript or JavaScript
  3. Paste your SQL CREATE TABLE statements into the input area
  4. Click "Generate Knex Migration" to create the migration file
  5. Copy the output and save it in your Knex migrations folder

Why Use This Tool?

Automatically map SQL types to Knex schema builder methods
Handles PRIMARY KEY, NOT NULL, DEFAULT, REFERENCES, and UNIQUE constraints
Generates both up and down migration functions
Supports all three Knex dialects: PostgreSQL, MySQL, and SQLite

Tips & Best Practices

  • SERIAL columns are mapped to table.increments() which auto-creates primary key
  • Foreign key references generate .references().inTable() chain
  • Down migration drops tables in reverse order to handle dependencies
  • Use TypeScript output for better type safety in your migration files

Frequently Asked Questions

What is Knex.js?

Knex.js is a SQL query builder for JavaScript that works with PostgreSQL, MySQL, SQLite, and other databases. It provides a flexible migration system, connection pooling, and a fluent API for building queries.

How are SQL types mapped to Knex methods?

SQL types are mapped to Knex schema builder methods. For example, VARCHAR maps to table.string(), INTEGER maps to table.integer(), TIMESTAMP maps to table.timestamp(). SERIAL types map to table.increments() which creates an auto-incrementing primary key.

What is the difference between up and down migrations?

The up migration creates tables and indexes, while the down migration reverses those changes by dropping tables. This allows you to rollback migrations when needed.

Is my SQL data sent to a server?

No, all processing happens entirely in your browser. Your SQL schema never leaves your device.

Related Tools