typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

migration fails with TypeError: constraint.clone is not a function

Open bockha opened this issue 2 years ago • 0 comments

Last week I had an issue with a new migration, that would not run with all previous migration at once on an empty database. Though it was running, if it was presented a database that had some of the previous migrations being run on. I managed to enclose the problem down to three migrations which I condensed to the code below which will reproduce the error. I also found out that I can prevent the error by calling queryRunner.getTable(tableName); right before the failing migration.

Issue Description

Expected Behavior

I expect the migration below to succeed without uncommenting await queryRunner.getTable(tableName); in the migration 2 of the code below.

Actual Behavior

The migration fails.

Migration "foo1663574872076" failed, error: constraint.clone is not a function
query: ROLLBACK
Error during migration run:
TypeError: constraint.clone is not a function
    at .../node_modules/typeorm/src/schema-builder/table/Table.ts:163:66
    at Array.map (<anonymous>)
    at Table.clone (.../node_modules/typeorm/src/schema-builder/table/Table.ts:163:35)
    at PostgresQueryRunner.addColumn (.../node_modules/typeorm/src/driver/postgres/PostgresQueryRunner.ts:876:35)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async foo1663574872076.up (.../apps/app-metadata-adapter/src/migrations/1663574872076-foo.js:32:5)
    at async MigrationExecutor.executePendingMigrations (.../node_modules/src/migration/MigrationExecutor.ts:262:17)
    at async DataSource.runMigrations (.../node_modules/src/data-source/DataSource.ts:377:13)
    at async Object.handler (.../node_modules/src/commands/MigrationRunCommand.ts:67:13)

Steps to Reproduce

  1. run the migration below on a (postgres) database and it will fail
  2. uncomment the line await queryRunner.getTable(tableName); of migration 2 and the whole migration will succeed
const { Table, TableColumn } = require('typeorm');

const tableName = 'foo';

const unique = { name: 'myUnique', columnNames: ['bar'] };

const quxName = 'qux';

module.exports = class foo1663574872076 {
  async up(queryRunner) {
    // migration 1
    await queryRunner.createTable(
      new Table({
        name: tableName,
        columns: [
          {
            name: 'id',
            type: 'integer',
            isPrimary: true,
            isGenerated: true,
          },
          { name: 'bar', type: 'varchar', isNullable: true },
        ],
      })
    );

    // migration 2
    await queryRunner.createUniqueConstraint(tableName, unique);
    // await queryRunner.getTable(tableName); // <- remove comment and migration 3 will succeed

    // migration 3 -> fails, error: constraint.clone is not a function
    await queryRunner.addColumn(
      tableName,
      new TableColumn({
        name: quxName,
        type: 'varchar',
        isNullable: true,
      })
    );
  }

  async down(queryRunner) {
    await queryRunner.dropColumn(tableName, quxName);

    await queryRunner.dropUniqueConstraint(tableName, unique.name);

    await queryRunner.dropTable(tableName);
  }
};

My Environment

Dependency Version
Operating System Linux
Node.js version v16.17.0
Typescript version 4.7.3
TypeORM version 0.3.6
nx/nrwl 14.4.3
nestjs 8.1.2

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

bockha avatar Sep 19 '22 09:09 bockha