{"id":"991ee0b6152a1e05","repo":"typeorm/typeorm","slug":"supplied-check-constraint-was-not-found-in-table-991ee0","errorCode":null,"errorMessage":"Supplied check constraint was not found in table ${table.name}","messagePattern":"Supplied check constraint was not found in table (.+?)","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"src/driver/sqlserver/SqlServerQueryRunner.ts","lineNumber":2543,"sourceCode":"     *\n     * @param tableOrName\n     * @param checkOrName\n     * @param ifExists\n     */\n    async dropCheckConstraint(\n        tableOrName: Table | string,\n        checkOrName: TableCheck | string,\n        ifExists?: boolean,\n    ): Promise<void> {\n        const table = InstanceChecker.isTable(tableOrName)\n            ? tableOrName\n            : await this.getCachedTable(tableOrName)\n        const checkConstraint = InstanceChecker.isTableCheck(checkOrName)\n            ? checkOrName\n            : table.checks.find((c) => c.name === checkOrName)\n        if (!checkConstraint) {\n            if (ifExists) return\n            throw new TypeORMError(\n                `Supplied check constraint was not found in table ${table.name}`,\n            )\n        }\n\n        const up = this.dropCheckConstraintSql(table, checkConstraint)\n        const down = this.createCheckConstraintSql(table, checkConstraint)\n        await this.executeQueries(up, down)\n        table.removeCheckConstraint(checkConstraint)\n    }\n\n    /**\n     * Drops check constraints.\n     *\n     * @param tableOrName\n     * @param checkConstraints\n     * @param ifExists\n     */\n    async dropCheckConstraints(","sourceCodeStart":2525,"sourceCodeEnd":2561,"githubUrl":"https://github.com/typeorm/typeorm/blob/04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96/src/driver/sqlserver/SqlServerQueryRunner.ts#L2525-L2561","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass true as the third argument (ifExists) for conditional drops.","Inspect table.checks on the cached table to confirm the exact constraint name.","When dropping an enum-derived check constraint, account for the CHK_..._ENUM naming pattern used by isEnumCheckConstraint."],"exampleFix":"// before\nawait queryRunner.dropCheckConstraint(\"users\", \"CHK_users_age\")\n// after\nawait queryRunner.dropCheckConstraint(\"users\", \"CHK_users_age\", true)","handlingStrategy":"validation","validationCode":"const table = await queryRunner.getCachedTable(tableName)\nconst exists = !!table?.checks.find(c => c.name === constraintName)\nawait queryRunner.dropCheckConstraint(tableName, constraintName, /* ifExists */ !exists)","typeGuard":"function checkConstraintExists(table: Table | undefined, name: string): boolean {\n  return !!table?.checks.some(c => c.name === name)\n}","tryCatchPattern":"try {\n  await queryRunner.dropCheckConstraint(table, name, true)\n} catch (e) {\n  if (e instanceof TypeORMError && /Supplied check constraint was not found/.test(e.message)) return\n  throw e\n}","preventionTips":["Use ifExists=true for conditional check-constraint drops.","Account for the _ENUM naming suffix when dropping enum-derived check constraints.","Inspect table.checks to confirm exact names before dropping."],"tags":["sqlserver","schema-migration","constraint-not-found","check"],"analyzedSha":"04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96","analyzedAt":"2026-08-03T18:27:32.281Z","schemaVersion":2}