typeorm/typeorm · error · TypeORMError

Supplied check constraint was not found in table ${table.nam

Error message

Supplied check constraint was not found in table ${table.name}

What it means

Thrown by SqlServerQueryRunner.dropCheckConstraint when the supplied check constraint name (or TableCheck object) is not found in table.checks and ifExists is not set. Check constraints are matched by .name. This guards against dropping a constraint that does not exist in the cached schema snapshot.

Source

Thrown at src/driver/sqlserver/SqlServerQueryRunner.ts:2543

     *
     * @param tableOrName
     * @param checkOrName
     * @param ifExists
     */
    async dropCheckConstraint(
        tableOrName: Table | string,
        checkOrName: TableCheck | string,
        ifExists?: boolean,
    ): Promise<void> {
        const table = InstanceChecker.isTable(tableOrName)
            ? tableOrName
            : await this.getCachedTable(tableOrName)
        const checkConstraint = InstanceChecker.isTableCheck(checkOrName)
            ? checkOrName
            : table.checks.find((c) => c.name === checkOrName)
        if (!checkConstraint) {
            if (ifExists) return
            throw new TypeORMError(
                `Supplied check constraint was not found in table ${table.name}`,
            )
        }

        const up = this.dropCheckConstraintSql(table, checkConstraint)
        const down = this.createCheckConstraintSql(table, checkConstraint)
        await this.executeQueries(up, down)
        table.removeCheckConstraint(checkConstraint)
    }

    /**
     * Drops check constraints.
     *
     * @param tableOrName
     * @param checkConstraints
     * @param ifExists
     */
    async dropCheckConstraints(

View on GitHub (pinned to 04ff4daedc)

Solutions

  1. Pass true as the third argument (ifExists) for conditional drops.
  2. Inspect table.checks on the cached table to confirm the exact constraint name.
  3. When dropping an enum-derived check constraint, account for the CHK_..._ENUM naming pattern used by isEnumCheckConstraint.

Example fix

// before
await queryRunner.dropCheckConstraint("users", "CHK_users_age")
// after
await queryRunner.dropCheckConstraint("users", "CHK_users_age", true)
Defensive patterns

Strategy: validation

Validate before calling

const table = await queryRunner.getCachedTable(tableName)
const exists = !!table?.checks.find(c => c.name === constraintName)
await queryRunner.dropCheckConstraint(tableName, constraintName, /* ifExists */ !exists)

Type guard

function checkConstraintExists(table: Table | undefined, name: string): boolean {
  return !!table?.checks.some(c => c.name === name)
}

Try / catch

try {
  await queryRunner.dropCheckConstraint(table, name, true)
} catch (e) {
  if (e instanceof TypeORMError && /Supplied check constraint was not found/.test(e.message)) return
  throw e
}

Prevention

When it happens

Trigger: Calling queryRunner.dropCheckConstraint(table, "CHK_name") where no check constraint with that name exists in the cached table. Happens when the constraint name was auto-generated (namingStrategy.checkConstraintName) and differs from the hardcoded string, or when it was already dropped.

Common situations: Mismatch between the naming-strategy-generated check constraint name and the name passed; re-running migrations after partial failures; branch divergence in migration history; forgetting to account for the _ENUM suffix that SQL Server appends for enum check constraints.

Related errors


AI-assisted analysis of typeorm/typeorm@04ff4daedc (2026-08-03). Data as JSON: /data/errors/991ee0b6152a1e05.json. Report an issue: GitHub.