Search code examples
node.jstypescriptpostgresqlnestjstypeorm

How to fix: No changes in database schema were found - cannot generate a migration?


I searched a lot and there are several questions like this however most of them do not have any answer or are not relevant to me.

I'm using TypeORM(v0.2.45) with Postgres driver and my entities/schemas are working fine with synchronize mode enabled.

My goal is to reverse generate migrations from the existing entities however it fails somehow.

This is what I get when trying to generate migrations

❯ npm run migration:generate Coffee

> [email protected] migration:generate
> npm run build && npm run typeorm migration:generate -- -n "Coffee"


> [email protected] prebuild
> rimraf dist


> [email protected] build
> cross-env NODE_ENV=production nest build


> [email protected] typeorm
> cross-env NODE_ENV=production ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config dist/src/common/setup/config/orm.config.js "migration:generate" "-n" "Coffee"

No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command

Here are my npm scripts for TypeORM

{
  "typeorm": "cross-env NODE_ENV=production ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config dist/src/common/setup/config/orm.config.js",
  "migration:generate": "npm run build && npm run typeorm migration:generate -- -n",
  "migration:run": "npm run typeorm migration:run"
}

orm.config.ts

import { Env } from '../../env';
import { join } from 'path';
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';

export default {
  database: Env.isTest ? ':memory:' : process.env.DB_DATABASE || 'dri',
  type: Env.isTest ? 'sqlite' : 'postgres',
  port: Number(process.env.DB_PORT || 5432),
  username: process.env.DB_USERNAME || 'dri-user',
  password: process.env.DB_PASSWORD || 'dri-secret',
  host: process.env.DB_HOST || '127.0.0.1',
  ...(!Env.isProd && {
    synchronize: true,
    synchronizeOptions: {
      force: true,
    },
  }),
  autoLoadEntities: true,
  entities: [Env.isTest ? 'src/**/*.entity{.ts,.js}' : join(__dirname, './**/*.entity{.ts,.js}')],
  keepConnectionAlive: true,
  namingStrategy: new SnakeNamingStrategy(),
  logging: Env.isDev ? 'all' : 'error',
  migrations: [join(__dirname, './**/*.entity{.ts,.js}')],
  cli: {
    migrationsDir: 'migrations',
  },
};

When I try to create a migration - it works but I want to generate it from the existing schema which is failing at the moment. 🤕

p.s.

I tried it with all the tables removed and also having em all in place but the result is the same - did not generate anything.😔


Solution

  • Late to the party but maybe it will help someone else.

    TypeOrm compares your entities schema to the database schema for any changes. If not changes between the two are found, it will not create a new migration. Because you are using sync: true, you DB is already up to date with your orm code so no migration is generated.

    To generate migrations for each table, you will need to delete one table at a time, run the generate command and after run the migration. Repeat for all the tables.