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:
Anyone has any idea as for why aren’t my tables being created? Any help would be appreciated.
2
Answers
You should try following script for
migration:generate
:Remove
default
because you’re using path: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.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:
Hope it helps!