{"id":12001,"library":"sequelize-typescript-migration-lts","title":"Sequelize TypeScript Migration LTS","description":"sequelize-typescript-migration-lts is a migration tool designed for Sequelize ORM users who leverage TypeScript for defining their database models. It automates the generation of database migration files by detecting schema changes based on the current state of Sequelize TypeScript models (which use decorators like `@Table` and `@Column`). The package then creates `up` and `down` functions for these changes, eliminating the need for manual migration script writing, similar to Django's `makemigration` feature. The current stable version is 3.2.5, with recent updates focusing on bug fixes and supporting features like snake-case column names. It's important to note that this tool is specifically built for `sequelize-typescript` models and does not support plain `sequelize` model definitions. The project is an LTS (Long-Term Support) fork, integrating improvements from various community forks of the original `sequelize-auto-migrations` project.","status":"active","version":"3.2.5","language":"javascript","source_language":"en","source_url":"https://github.com/mmRoshani/sequelize-typescript-migration","tags":["javascript","sequelize","typescript","migrate","migration","makemigration","diff","compare","detect"],"install":[{"cmd":"npm install sequelize-typescript-migration-lts","lang":"bash","label":"npm"},{"cmd":"yarn add sequelize-typescript-migration-lts","lang":"bash","label":"yarn"},{"cmd":"pnpm add sequelize-typescript-migration-lts","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Underlying ORM required for database interaction and model definitions.","package":"sequelize","optional":false},{"reason":"Core dependency for defining models with TypeScript decorators, which the migration tool introspects.","package":"sequelize-typescript","optional":false}],"imports":[{"note":"This is the primary class for generating migrations. The package primarily uses ESM imports.","wrong":"const SequelizeTypescriptMigration = require('sequelize-typescript-migration-lts');","symbol":"SequelizeTypescriptMigration","correct":"import { SequelizeTypescriptMigration } from 'sequelize-typescript-migration-lts';"},{"note":"Type import for configuring the `makeMigration` method. Available since v3.x.","symbol":"MakeMigrationOptions","correct":"import type { MakeMigrationOptions } from 'sequelize-typescript-migration-lts';"}],"quickstart":{"code":"import { join } from 'path';\nimport { Sequelize } from \"sequelize-typescript\";\nimport { SequelizeTypescriptMigration, MakeMigrationOptions } from \"sequelize-typescript-migration-lts\";\n\nconst sequelize = new Sequelize({\n  dialect: 'sqlite',\n  storage: './database.sqlite',\n  models: [join(__dirname, 'models')], // Ensure your models are loaded\n  logging: false,\n});\n\n// Define a sample model in models/CarBrand.ts\n// import { Table, Column, Model, DataType, Default } from 'sequelize-typescript';\n// @Table\n// export class CarBrand extends Model<CarBrand> {\n//   @Column\n//   name: string;\n//   @Default(true)\n//   @Column(DataType.BOOLEAN)\n//   isCertified: boolean;\n// }\n\nasync function runMigration() {\n  try {\n    const options: MakeMigrationOptions = {\n      outDir: join(__dirname, './migrations'),\n      migrationName: 'initial-schema-setup',\n      preview: false,\n      isTs: true, // Generate TypeScript migration files\n    };\n    await SequelizeTypescriptMigration.makeMigration(sequelize, options);\n    console.log('Migration generated successfully. Run `npx sequelize-cli db:migrate` to apply.');\n  } catch (error) {\n    console.error('Error generating migration:', error);\n  }\n}\n\n// In a real application, you'd integrate this with your CLI or startup process.\n// For demonstration, we directly call it.\n// Make sure your Sequelize instance is initialized and models are defined before calling this.\n// For a quick test, you might call `runMigration()` after defining models.\n// e.g., runMigration();","lang":"typescript","description":"This example demonstrates how to programmatically generate a new database migration file using `SequelizeTypescriptMigration.makeMigration`. It sets up a Sequelize instance, specifies an output directory for the migration, and names the migration file. This code should be executed after defining your Sequelize TypeScript models."},"warnings":[{"fix":"Refer to the compatibility section in the package's documentation to ensure the correct version of `sequelize-typescript-migration-lts` is installed for your `sequelize` ORM version.","message":"The package has specific compatibility requirements with `sequelize` versions. Use version `~2.0.0` of this package for `sequelize@~6.0.0`, and `~1.0.0` for `sequelize@~4.0.0`.","severity":"breaking","affected_versions":"All"},{"fix":"Before applying `down` migrations in production or critical environments, review the generated `down` functions for complex relational changes and manually adjust the order of operations if necessary.","message":"The `undo` (down) action of generated migrations may sometimes fail, particularly due to the ordering of model relations. Manual modification of the generated migration file might be necessary to resolve these issues.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure all models intended for migration generation are defined using `sequelize-typescript` decorators (`@Table`, `@Column`, etc.) and are registered correctly with your `sequelize-typescript` instance.","message":"This tool strictly supports models defined with `sequelize-typescript` decorators and does not work with plain `sequelize` models.","severity":"gotcha","affected_versions":"All"},{"fix":"Always review the generated migration files (both `up` and `down` functions) before applying them, especially for significant schema alterations, to catch any potential issues or inefficiencies.","message":"While the tool automates migration generation, it may not always produce perfect migration scripts for highly complex schema changes. Manual review and adjustments of the generated files are often prudent.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure your `Sequelize` instance is configured with the correct `models` path, typically using `models: [path.join(__dirname, 'models')]`, and that all models have `@Table` and `@Column` decorators.","cause":"The migration tool cannot locate or parse your Sequelize TypeScript models, likely due to incorrect model loading or missing decorators.","error":"Error: Model not found for table 'your_table_name'"},{"fix":"Verify the installation with `npm i sequelize-typescript-migration-lts` and ensure the import statement is `import { SequelizeTypescriptMigration } from 'sequelize-typescript-migration-lts';`.","cause":"The `SequelizeTypescriptMigration` class was not imported correctly or the package was not properly installed, leading to `makeMigration` being called on an undefined object.","error":"TypeError: Cannot read properties of undefined (reading 'makeMigration')"},{"fix":"Inspect the failing `down` function in the generated migration file. Manually adjust the SQL or Sequelize queries to ensure proper reversal of changes, paying close attention to relational dependencies and data integrity.","cause":"The generated `down` function in a migration script encountered an error, possibly due to incorrect SQL logic or issues with foreign key constraints order during rollback, as noted in the documentation.","error":"Migration 'XXXXXXXXXXXXXX-your-migration-name.js' failed: ... (during db:migrate:undo)"}],"ecosystem":"npm"}