Alojarf
Alojarf

Reputation: 61

TypeORM migration is not creating the required tables, instead it creates only a table called migration

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.

Upvotes: 2

Views: 4224

Answers (3)

PsuedoMaverick
PsuedoMaverick

Reputation: 21

In your package.json file update the generate and run commands to these:

"migration:generate": "ts-node node_modules/typeorm/cli.js migration:generate -d ormconfig.ts migrations/$npm_config_name",
"migration:run": "ts-node node_modules/typeorm/cli.js migration:run -d ormconfig.ts",

And in your ormconfig.ts update entities and migrations to:

entities: [__dirname + '/src/**/entities/*.entity{.ts,.js}'],
migrations: [__dirname + '/migrations/**/*{.ts,.js}'],

What's happening here is that the generate command tells orm that ormconfig is on the root level and there's a folder named migrations and create a file in that folder.

The ormconfig defines where the entities are and where the migrations will be.

What I think the problem might be is some path issues of pointing where the migrations actually are.

Upvotes: 0

Manuluks
Manuluks

Reputation: 31

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!

Upvotes: 3

Tai Lu
Tai Lu

Reputation: 43

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.

Upvotes: 0

Related Questions