typeorm: migrations doesnt run even after doing everything
Issue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem:
I am working on a project with typeorm on nestjs. the problem is even after running mgration after generating migration files, i get No migrations are pending
The commands i am using are
"typeorm": "NODE_ENV=dev ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/config/typeorm.config.ts",
"typeorm:migrate": "npm run typeorm migration:generate -- -n",
"typeorm:run": "npm run typeorm migration:run"
I am doing
npm run typeorm:migrate test
npm run typeorm:run
BTW: my ormconfig looks like this
import config from '@config/index';
import { join } from 'path';
import { ConnectionOptions } from 'typeorm';
const typeormConfig: ConnectionOptions = {
type: 'postgres',
host: config.db.host,
port: config.db.port,
logging: ['error'],
username: config.db.username,
password: config.db.password,
database: config.db.database,
entities: [join(__dirname, '../entities/**{.ts,.js}')],
synchronize: false,
migrations: ['migrations/**/*{.ts,.js}'],
cli: {
migrationsDir: 'migration',
},
};
export = typeormConfig;
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 33 (2 by maintainers)
This
No migrations are pendingmeans that somewhere in the migration table it says that you have already run the migration for your migration file. Follow this steps:distfolder (Just to make sure you dont get anDuplicated migrationerror).migrationtable.Hope this solves you problem.
This is what I am using as the ormconfig.ts
From this we started using dotenv as well for configuration with a file kinda like this:
One tip that isn’t immediately obvious if you are using dotenv and use any of the typeorm default env variables like these:
The cli will use those over the top of your ormconfig.ts for whatever reason so if that is not the behaviour you want then you need to alter your .env.
If you are wondering what that database-index.ts thing we have going on is it is (simplified):
Make sure if you are using typescript for the config you pass it as a param in along with build options for typeorm. I was able to get mine working with in package.json
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts","typeorm:migrate": "npm run typeorm migration:generate -- -n","typeorm:run": "npm run typeorm migration:run",Try to update paths to load entities and migrations from dist (build folder). For example
this is caused that the script cannt find any migration files
I’ve solved like this
ormconfig.jsrunning with ts-node
running with node
cli usage also runs good with
yarn exec -- ts-node node_modules/.bin/typeorm migration:revertNotice if you need run cli without ts-node, the
clisection should be also properly configured.I had similar experiences of not seeing migrations in a NextJS / TypeORM / TS project. This is what I’m doing now:
For migration files in TS:
For migration files in JS (also, production build and database):
So I guess
TYPEORM_MIGRATIONSandTYPEORM_URLare the minimum requirements to run migrations.TypeORM document menthions some rules on TypeORM config files and env-vars: https://github.com/typeorm/typeorm/blob/master/docs/using-ormconfig.md#which-configuration-file-is-used-by-typeorm. I didn’t understand them all but this setting works for me now.
@danieldspx Thanks for your tips!
I’m new in the typeorm and back-end world. The step 1 give to me the answer about why I having duplicated migration files.
After generating the initial migration. Have you made sure to run it before making changes to the entities?
You should see a table in postgres called migration that lists all of the migrations that have been run.
Make sure
migrationsRun: falsein config.Create inital migration:
npm run typeorm:migrate InitialMigrationRun initial migration:
npm run typeorm:runMake changes to an entity.ts
npm run typeorm:migrate MyChangesRun the new migration:
npm run typeorm:runIt should give you something like this in the migration files
It could have creates / updates / deletes etc depending on what you have changed.
Hi @rubiin I have taken a look at your repo and it seems pretty standard. I have some suggestions that you can try to rule out some issues. If you have already tried these apologies.
Firstly I notice in your
typeorm.config.tsyour migrations directory typeormConfig.cli.migrationsDir doesn’t seem correct as there doesn’t seem to be a folder calledsrc/Migrationsin your project.I also notice you have
migrationsRun: truethis will cause nest to auto-run them on startup rather than defering to the cli migration.Does your db connect in general? I see synchronize is set to false. If it is set to true is it actually writing your db changes?
Next have you tried directly linking your typescript entity files / migration files in your config like I have in the comment above with dbindex ^ https://github.com/typeorm/typeorm/issues/5103#issuecomment-602058732
We need to rule out tooling differences / typescript versions etc next so let me know.