Felipe Calderano
Felipe Calderano

Reputation: 171

NestJS, PortsgreSQL and TypeORM - Migrations not running properly

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?

Upvotes: 3

Views: 8641

Answers (3)

Milad Ashoori
Milad Ashoori

Reputation: 61

I think this one is to synchronize the database with the schema, and it's not a good idea. "synchronize": true,

Upvotes: 0

Felipe Calderano
Felipe Calderano

Reputation: 171

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.

Upvotes: 3

FULL STACK DEV
FULL STACK DEV

Reputation: 15941

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.

Upvotes: 2

Related Questions