Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeORM migration:generate finds no changes to put in the migration

Errortypeorm migration:generate finds no changes in the database schema, writes no file and exits with code 1

Short answer

migration:generate compares the entities your DataSource loads with the real database, and writes only the difference. No changes means that difference is empty: either the database already has your entity changes (often because synchronize ran), or the DataSource loads no entities, or it points at another database.

  • Applies to TypeORM 0.3, NestJS 11, Node.js 22, PostgreSQL 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print which entities the DataSource loads and which database it connects to. Run it from a small script that imports the same DataSource file you pass to -d.

    // check-ds.ts
    import dataSource from './src/data-source';
    dataSource.initialize().then((ds) => {
      console.log(ds.options.database, ds.entityMetadatas.map((e) => e.name));
      return ds.destroy();
    });
  2. 2Match what you see, then jump to its fix

    1. 1The new column or table already exists in the database before you ran any migrationsynchronize already changed the databaseFix
    2. 2The first step prints an empty entity listThe DataSource passed to -d does not list your entitiesFix
    3. 3The database name printed by the first step is not the one your app usesThe CLI connects to a different database than you expectFix
    4. 4You edited a TypeScript class but not a decorator, column or relationThe entity change is not something a migration recordsFix

What does the error look like?

  • typeorm migration:generate finds no changes in the database schema, writes no file and exits with code 1

Cause 1 of 4

synchronize already changed the database

1Check

With synchronize: true, TypeORM updates the schema every time the app starts, so the database already matches the entities and nothing is left to generate.

grep -rn "synchronize" src

2Fix

Turn synchronize off, as the migrations guide requires. To get a migration for the change that was already applied, point the DataSource at a database that does not have it yet (for example a fresh copy of production) and generate again.

export default new DataSource({
  // ...connection options
  synchronize: false,
  migrations: ['src/migrations/*.ts'],
});

3Verify

Generate against a database that still has the old schema. A migration file is written.

npm run typeorm migration:generate -- -d src/data-source.ts src/migrations/AddColumn

Confirmed by typeorm.io

Did this fix it?

Cause 2 of 4

The DataSource passed to -d does not list your entities

1Check

The entities option takes entity classes or paths and globs. An empty or wrong path loads nothing, so there is nothing to compare.

grep -n "entities" src/data-source.ts

2Fix

List the entity classes or a glob that matches your entity files. Use a glob that also matches the file type the CLI runs (TypeScript with ts-node, JavaScript after a build).

entities: ['src/**/*.entity.ts'],

3Verify

Run the first-step script again: the entity names appear.

npx ts-node check-ds.ts

Confirmed by typeorm.io

Did this fix it?

Cause 3 of 4

The CLI connects to a different database than you expect

1Check

The DataSource file used by the CLI often has its own connection settings, separate from the Nest app, and may read a different environment file.

grep -n "database\|host\|url" src/data-source.ts

2Fix

Read the same environment variables in both places, or build both configs from one shared object.

// src/data-source.ts
import 'dotenv/config';
export default new DataSource({
  type: 'postgres',
  url: process.env.DATABASE_URL,
  entities: ['src/**/*.entity.ts'],
  synchronize: false,
});

3Verify

The first-step script prints the database your app uses.

npx ts-node check-ds.ts

Confirmed by typeorm.io

Did this fix it?

Cause 4 of 4

The entity change is not something a migration records

1Check

Only changes in the schema are detected. Changing a method, a type that is not a column, or code outside the entity does not alter the database.

git diff -- '*.entity.ts'

2Fix

Add or change the column, relation or index decorator you meant to. If you need a data change or custom SQL, create an empty migration and write it yourself.

npm run typeorm migration:create src/migrations/BackfillData

3Verify

Generate again after the decorator change.

npm run typeorm migration:generate -- -d src/data-source.ts src/migrations/Change

Confirmed by typeorm.io

Did this fix it?

How do I stop it coming back?

  • Keep synchronize: false everywhere once you use migrations.
  • Use one DataSource file for the CLI and import it into the Nest module config.
  • Add a package.json script "typeorm": "typeorm-ts-node-commonjs" (or typeorm-ts-node-esm) so the CLI can read TypeScript entities.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

NestJS GraphQL cannot work out the output type of a field or query

Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.

In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.