0

I am new this framework Nest JS and created sample Restful API but could not migrate the entity file. I tried many ways it's not working. first time run migration command to generate migration file it's working fine. but second time creating a new entity then run the migration command it's show no changes message how to fix this Issue.

DB config .ts file

{
            type: 'postgres',
            host: process.env.POSTGRES_HOST,
            port: parseInt(process.env.POSTGRES_PORT) || 5432,
            database: process.env.POSTGRES_DATABASE,
            username: process.env.POSTGRES_USER,
            password: process.env.POSTGRES_PASSWORD,
            entities: ["dist/**/*.entity{ .ts,.js}"],
            synchronize: true,
            "migrations": ["dist/migrations/*{.ts,.js}"],
            "migrationsTableName": "migrations_typeorm",
            // ssl:{
            //     rejectUnauthorized:false
            // }
          }

Migration Command

Create and generate migration command

npx typeorm migration:create -n User -d src/migrations

npx typeorm migration:generate -n User -d src/migrations

Run Migration File

npx typeorm migration:run

1 Answers1

0

You should only use migration:generate in your case. This will check your current database structure and compare it against entity file and based on this generate a new migration file with changes.

Problem that you were experiencing was porbably due to override.

migration:create will generate a new empty file and you run migration:generate it will not overwrite that file.

In case you want to write your own migration you can use migration:create as a simple and easy way to name it and structure it automatically.

Edit:

Also make sure to set synchronize to false as it will automatically migrate your data based on entities and changes otherwise.

I always turn this feature off and generate or write migration manually as it gives the most control.

Marko
  • 1
  • 2