# Sequelize CLI Migrations — Prisma / TypeORM / Sequelize

Source: https://www.skillbyai.com/en/orms/or-sequelize-migrations

> Write Sequelize migrations by hand with queryInterface, run and undo them with sequelize-cli, and keep models and migrations in step.

## Hand-written, model-independent

Sequelize's migration tool is the separate **`sequelize-cli`** package. `npx sequelize-cli init` creates `config/`, `models/`, `migrations/` and `seeders/` folders. Unlike Prisma and TypeORM, the Sequelize CLI **does not diff your models against the database**: `migration:generate` creates an empty skeleton and you write the changes yourself using **`queryInterface`** methods such as `createTable`, `addColumn`, `addIndex` and `removeColumn`. Each migration exports `up` and `down` functions. `db:migrate` runs pending ones and records them in the `SequelizeMeta` table; `db:migrate:undo` reverts the latest; `db:migrate:status` lists what has run. The cost of hand-writing is that models and migrations can drift apart: you add `bio` to the model but forget the migration, and queries fail at runtime. The benefit is that nothing destructive is ever generated behind your back. Keep the habit of writing the migration in the same commit as the model change.

## Adding a column with queryInterface

Generated with `npx sequelize-cli migration:generate --name add-bio-to-users`, then filled in by hand.

```js
'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.addColumn('Users', 'bio', {
      type: Sequelize.TEXT,
      allowNull: true,
    });
  },

  async down(queryInterface) {
    await queryInterface.removeColumn('Users', 'bio');
  },
};

// terminal: npx sequelize-cli db:migrate
```

## Writing your own exam answer key

Prisma and TypeORM are like a teacher who drafts the answer key for you to check. Sequelize CLI hands you a blank sheet. More effort, but you know every line on it because you wrote it.

**Quiz:** You added a phone attribute to a Sequelize model and ran db:migrate, but INSERTs fail with 'column phone does not exist'. Why?

- [x] sequelize-cli does not generate migrations from model changes, so no migration added the column
- [ ] db:migrate only runs in production
- [ ] Sequelize requires a restart of the database server
- [ ] The phone attribute must be declared in SequelizeMeta

*Answer:* sequelize-cli does not generate migrations from model changes, so no migration added the column. Sequelize CLI migrations are written by hand. Without a migration calling addColumn, the table never gained the column even though the model mentions it.
