You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exports.up=function(knex){returnknex.schema.createTable('products',function(table){table.increments('id').primary();// Auto IDtable.string('name',150).notNullable().unique();// Required + unique nametable.text('description');// Optionaltable.decimal('price',10,2).notNullable();// Currencytable.integer('stock').notNullable().defaultTo(0);// Inventory counttable.boolean('is_active').notNullable().defaultTo(true);// Statustable.enu('status',['draft','published','archived'])// Enum status.notNullable().defaultTo('draft');table.uuid('vendor_id')// UUID foreign key.references('id').inTable('vendors').onDelete('SET NULL').onUpdate('CASCADE');table.jsonb('metadata');// Searchable JSONtable.timestamp('available_from',{useTz: true});// Optional timestamptable.timestamps(true,true);// created_at, updated_attable.index(['vendor_id']);// Index for fast filtering});};exports.down=function(knex){returnknex.schema.dropTable('products');};---
# β MigrationTableExampleβPostgreSQLOptimized```js// migrations/20250503121000_create_users_and_profiles.js/** * This migration demonstrates common table options in Knex. */exportasyncfunctionup(knex){awaitknex.schema.createTable('users',(table)=>{table.increments('id').primary();// Auto-incrementing primary keytable.string('username',50).notNullable().unique();// String with max length, required, uniquetable.string('email').notNullable().unique();// Unique emailtable.string('password_hash').notNullable();// Password hashtable.boolean('is_active').defaultTo(true);// Boolean flag with defaulttable.timestamp('created_at').defaultTo(knex.fn.now());// Timestamp with defaulttable.timestamp('updated_at').defaultTo(knex.fn.now());});awaitknex.schema.createTable('profiles',(table)=>{table.increments('id');table.integer('user_id').unsigned().references('id').inTable('users').onDelete('CASCADE');table.text('bio');// Text columntable.string('avatar_url');// Optional stringtable.date('birthdate');// Date typetable.enu('gender',['male','female','other']);// Enumtable.jsonb('preferences');// JSONB column (PostgreSQL only)table.timestamps(true,true);// created_at and updated_at with defaults});}/** * Drops both tables if rolled back. */exportasyncfunctiondown(knex){awaitknex.schema.dropTableIfExists('profiles');awaitknex.schema.dropTableIfExists('users');}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npx knex migrate:make create_users_table
# Creates a new migration file with the name "create_users_table" to define changes to the database schema.
npx knex migrate:latest --knexfile db/knexfile.js
# Runs the latest migrations, applying any unapplied migrations to the database.
npx knex migrate:make migration_name --knexfile db/knexfile.js
# Creates a new migration file with the given "migration_name". This is the base file to define schema changes.
npx knex seed:make seed_name --knexfile db/knexfile.js
# Creates a new seed file with the name "seed_name" to populate the database with initial data.
npx knex seed:run --knexfile db/knexfile.js
# Executes all seed files, populating the database with data defined in the seed files.
npx knex migrate:up migration_name --knexfile db/knexfile.js
# Applies a specific migration by the name "migration_name". This is used when you want to manually run a migration.
npx knex migrate:down migration_name --knexfile db/knexfile.js
# Rolls back a specific migration by the name "migration_name". This is used to undo a previously applied migration.
npx knex migrate:rollback --all --knexfile db/knexfile.js
# Rolls back all migrations, undoing every migration that has been applied, essentially resetting the schema to its initial state.### Migration Example```jsexports.up = function(knex) {return knex.schema.createTable('users', function(table) {table.increments('id').primary(); table.string('name');table.string('email').unique(); });};exports.down = function(knex) {return knex.schema.dropTableIfExists('users');};
Run Migrations
npx knex migrate:latest
Roll Back Migrations
npx knex migrate:rollback # Roll back latest batch
npx knex migrate:rollback --all # Roll back all