skip to Main Content

I am building a backend application using typescript, typeORM and postgres, after generating and running my migrations instead of creating the tables of the entities I wrote, it only creates a single empty table of 3 columns called ‘migrations’

I don’t understand why that happens. Here follows the command I used to generate my migrations:

"migration:generate": "typeorm-ts-node-commonjs -d ./src/database/data-source.ts migration:generate ./src/migration/default"

This is the command I used to run the migrations:

"migration:run": "typeorm-ts-node-commonjs -d ./src/database/data-source.ts migration:run"

And this is what my code looks like:

this is my DataSource instance:

export const AppDataSource = new DataSource({
 type: "postgres",
 host: process.env.DB_HOST,
 port: Number(process.env.DB_PORT),
 username: process.env.DB_USER,
 password: process.env.DB_PASSWORD,
 database: process.env.DB_NAME,
 synchronize: false,
 logging: false,
 entities: [`${__dirname}/**/entity/*.{ts,js}`],
 migrations: [`${__dirname}/**/migration/*.{ts,js}`],
});

I apologize for printing instead of pasting the code here, but since the migration looks pretty big, I will leave a print of it here in case anything in there helps to identify the issue. Also the migration was automatically generated:

migration

Anyone has any idea as for why aren’t my tables being created? Any help would be appreciated.

2

Answers


  1. You should try following script for migration:generate:

    "migration:generate": "typeorm-ts-node-commonjs -d ./src/database/data-source.ts -- migration:generate ./src/migration/"
    

    Remove default because you’re using path:

    export const AppDataSource = new DataSource({
     ...
     migrations: [`${__dirname}/**/migration/*.{ts,js}`],
    });
    

    After running migration:generate then check any file *.ts created in ./src/migration and view changes (If it don’t work, please check entities and migrations path).

    Finally, run migration:run to apply changes to db.

    Login or Signup to reply.
  2. The commands you are using seem to be correct, and when you run a migration, a record of it should be added to your database and the tables and data in your database should be created as expected. The issue may be with the migrations parameter in your data source configuration. If you are having the same problem as I did, where migrations located in "src/migration/" were not being executed, try using the following setting for the migrations parameter:

      migrations: ["src/migration/**/*.ts"],
    

    Hope it helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search