{"id":"338d31c8fc3edad9","repo":"typeorm/typeorm","slug":"supplied-foreign-key-was-not-found-in-table-tabl-338d31","errorCode":null,"errorMessage":"Supplied foreign key was not found in table ${table.name}","messagePattern":"Supplied foreign key was not found in table (.+?)","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"src/driver/oracle/OracleQueryRunner.ts","lineNumber":2263,"sourceCode":"     *\n     * @param tableOrName\n     * @param foreignKeyOrName\n     * @param ifExists\n     */\n    async dropForeignKey(\n        tableOrName: Table | string,\n        foreignKeyOrName: TableForeignKey | string,\n        ifExists?: boolean,\n    ): Promise<void> {\n        const table = InstanceChecker.isTable(tableOrName)\n            ? tableOrName\n            : await this.getCachedTable(tableOrName)\n        const foreignKey = InstanceChecker.isTableForeignKey(foreignKeyOrName)\n            ? foreignKeyOrName\n            : table.foreignKeys.find((fk) => fk.name === foreignKeyOrName)\n        if (!foreignKey) {\n            if (ifExists) return\n            throw new TypeORMError(\n                `Supplied foreign key was not found in table ${table.name}`,\n            )\n        }\n\n        foreignKey.name ??= this.dataSource.namingStrategy.foreignKeyName(\n            table,\n            foreignKey.columnNames,\n            this.getTablePath(foreignKey),\n            foreignKey.referencedColumnNames,\n        )\n\n        const up = this.dropForeignKeySql(table, foreignKey)\n        const down = this.createForeignKeySql(table, foreignKey)\n        await this.executeQueries(up, down)\n        table.removeForeignKey(foreignKey)\n    }\n\n    /**","sourceCodeStart":2245,"sourceCodeEnd":2281,"githubUrl":"https://github.com/typeorm/typeorm/blob/04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96/src/driver/oracle/OracleQueryRunner.ts#L2245-L2281","documentation":"Thrown by OracleQueryRunner.dropForeignKey when no foreign key matching the supplied name/object is present in the table's cached metadata (table.foreign_keys). The runner first resolves the argument: if it is a TableForeignKey it is used directly, otherwise it searches table.foreignKeys by .name. The optional 'ifExists' flag suppresses the throw when true.","triggerScenarios":"Calling queryRunner.dropForeignKey(table, \"fk_name\") where 'fk_name' is not among table.foreignKeys[].name. Commonly caused by a mismatched auto-generated FK name (naming strategy), a typo, or dropping an already-removed FK.","commonSituations":"Naming-strategy changes between migrations; manual DDL applied outside TypeORM that dropped the FK; double-drop in sequenced migrations; schema-qualified vs bare name mismatch.","solutions":["Pass ifExists: true (third argument) to make the drop idempotent.","Confirm the FK name against ALL_CONSTRAINTS WHERE CONSTRAINT_TYPE='R' or via table.foreignKeys before calling.","If passing a TableForeignKey object, ensure its .name exactly matches the cached metadata name.","Re-run schema load / use getCachedTable so the metadata reflects current DB state."],"exampleFix":"// before\nawait queryRunner.dropForeignKey(\"orders\", \"fk_orders_user\");\n\n// after\nawait queryRunner.dropForeignKey(\"orders\", \"fk_orders_user\", true);","handlingStrategy":"validation","validationCode":"const table = await queryRunner.getTable(\"orders\");\nconst fkName = \"fk_orders_user\";\nif ((table?.foreignKeys ?? []).some(fk => fk.name === fkName)) {\n  await queryRunner.dropForeignKey(\"orders\", fkName);\n}","typeGuard":"import { TableForeignKey } from \"typeorm\";\nfunction isTableForeignKey(o: unknown): o is TableForeignKey {\n  return typeof o === \"object\" && o !== null\n    && Array.isArray((o as any).columnNames)\n    && Array.isArray((o as any).referencedColumnNames);\n}","tryCatchPattern":null,"preventionTips":["Pass ifExists:true for idempotent FK drops.","Resolve FK names from table.foreignKeys to avoid string typos.","Make migrations idempotent so re-runs never throw on absent FKs."],"tags":["oracle","foreign-key","schema","migration"],"analyzedSha":"04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96","analyzedAt":"2026-08-03T18:27:32.281Z","schemaVersion":2}