Search code examples
postgresqltypeorm

TypeORM migration gives Maximum call stack size exceeded error with Postgres


When I run a migration, I get the following error. I initially had this in one project - now I've created a new blank project and still get the error. Here is the ormconfig.ts:


import { DataSource } from 'typeorm';

const env = {
  "DB_HOST":"localhost",
  "DB_PORT":5432,
  "DB_USERNAME":"postgres",
  "DB_PASSWORD":"postgres",
  "DB_DATABASE":"task-management",
}

export const connectionSource = new DataSource({
  migrationsTableName: 'migrations',
  type: 'postgres',
  host: env.DB_HOST,
  port: env.DB_PORT,
  username: env.DB_USERNAME,
  password: env.DB_PASSWORD,
  database: env.DB_DATABASE,
  logging: false,
  synchronize: false,
  name: 'default',
  migrations: ['migrations/**/*{.ts,.js}'],
});

I run the migration with: typeorm-ts-node-esm migration:run -d migrations/ormconfig.ts

which gives the following error:


 Error during migration run:
    RangeError: Maximum call stack size exceeded
        at /Users/christianayscue/Desktop/nestjsClass/nestjs-task-management/node_modules/src/util/DirectoryExportedClassesLoader.ts:29:43
        at Array.forEach (<anonymous>)
        at loadFileClasses (/Users/christianayscue/Desktop/nestjsClass/nestjs-task-management/node_modules/src/util/DirectoryExportedClassesLoader.ts:29:35)
        at /Users/christianayscue/Desktop/nestjsClass/nestjs-task-management/node_modules/src/util/DirectoryExportedClassesLoader.ts:27:42
        at Array.forEach (<anonymous>)
        at loadFileClasses (/Users/christianayscue/Desktop/nestjsClass/nestjs-task-management/node_modules/src/util/DirectoryExportedClassesLoader.ts:27:22)
        at /Users/christianayscue/Desktop/nestjsClass/nestjs-task-management/node_modules/src/util/DirectoryExportedClassesLoader.ts:30:17
        at Array.forEach (<anonymous>)
        at loadFileClasses (/Users/christianayscue/Desktop/nestjsClass/nestjs-task-management/node_modules/src/util/DirectoryExportedClassesLoader.ts:29:35)
        at /Users/christianayscue/Desktop/nestjsClass/nestjs-task-management/node_modules/src/util/DirectoryExportedClassesLoader.ts:30:17

I assume there is some circular dependency causing an infinite loop in DirectoryExportedClassesLoader.ts, so I put a console.log(JSON.stringify(exported)) on line 26 of DirectoryExportedClassesLoader.ts, and I now get:

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'DataSource'
    |     property 'driver' -> object with constructor 'PostgresDriver'
    --- property 'connection' closes the circle
    at JSON.stringify (<anonymous>)
    at loadFileClasses (/Users/christianayscue/Desktop/nestjsClass/typeormTest/src/util/DirectoryExportedClassesLoader.ts:29:25)

It seems DirectoryExportedClassLoader.js is guaranteed to get a Maximum call stack size exceeded error if there are circular dependencies, because it is self-recursive whenever it encounters an object property.

A little help please!


Solution

  • changing this entry from the DataSource config object fixed the issue.

    migrations: []
    

    This was one of those ones that just took a lot of trial and error to solve...