简体   繁体   中英

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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM