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
- Select your database dialect (PostgreSQL, MySQL, or SQLite)
- Choose output language: TypeScript or JavaScript
- Paste your SQL CREATE TABLE statements into the input area
- Click "Generate Knex Migration" to create the migration file
- Copy the output and save it in your Knex migrations folder
Why Use This Tool?
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.