2
votes

When trying to run the TypeORM Migrations, either automatically in the application startup or manually via the TypeORM CLI, only the migrations table gets created (and it stays empty). The migration files themselves are not being executed.

Here is my tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

Here is my package.json

...
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
...

Here is my ormconfig.json

...
"entities": ["dist/**/*.entity{.ts,.js}"],
"synchronize": true,
"migrationsRun": true,
"migrations ": ["dist/migrations/*{.ts,.js}"],
"cli": {
    "migrationsDir": "src/migrations"
  }
...

The migration files are being created through the TypeORM CLI and they are to populate some tables (insert statements). They are not related to changes in the database schema.

Please, can anyone help me make it work?

2
run this command npx typeorm migration:run - Adnan Mumtaz
I tried it but the only thing that happens is that the migrations table gets created (if it's not yet created). It's as if the migration files I created with the insert statements are not being found. I checked "migrations": ["dist/migrations/*{.ts,.js}"]and the files (.ts and .js) are all there. - Felipe Calderano

2 Answers

2
votes

That was a silly one! I guess some times the simplest problems are the hardest to spot.

The problem was in the ormconfig.json file. I removed this empty space ("migrations ":) and everything worked just fine.

0
votes

you should have synchronized to false synchronize:false

And from the terminal run

npx typeorm migration:generate -n AnyNameYouWant

After that, you can run

npx typeorm migration:run

You may also have to run nest build before running these commands.