OpenAPIBase
Bugs

TypeORM - Duplicate migrations issue

Christos Zioutas
#api#repo#framework#typeorm

While working with TypeORM if you follow the basic tutorials and even the documentation on their website, you will end up facing the following error. But why?

typeorm migration:run

query: SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'abc' AND TABLE_NAME = 'migrations'

query: SELECT * FROM abc.migrations migrations ORDER BY id DESC

Error during migration run:
Error: Duplicate migrations:

Let’s examine the error first. It mentions that it tries/it has found a migration which is duplicate and therefore it crashes. Yes it could just ignore it, but I believe they have correctly decided to stop the migration process as this can have cascading effects if it was just let lose.

Whenever you execute migration:run it also builds the application, which puts most content into the dist folder as well. This puts the migration files as well, but in .js instead of .ts.

Now most tutorials when instructing you how to setup the typeOrm.config.ts, they will suggest to configure the migration property like this migrations: ["**/migrations/*{.ts,.js}"],. With a quick inspection (if we actually check what we are copy pasting) it suggests to look anywhere in our codebase for the migrations either in .js or .ts extensions. Due to this global check, it will find the migrations twice, once in our source and once in our dist folder.

Ideally you adapt the migrations property to look only at your dist folder like this migrations: [‘dist/migrations/*{.ts,.js}’], and alas, the duplication issue is gone!

← Back to Blog