{"record":{"id":"d8f4208205ec2938","repo":"n8n-io/n8n","slug":"supplied-foreign-key-was-not-found-in-table-tabl","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":"packages/@n8n/typeorm/src/driver/sqlite-abstract/AbstractSqliteQueryRunner.ts","lineNumber":960,"sourceCode":"\n\t\tawait this.recreateTable(changedTable, table);\n\t}\n\n\t/**\n\t * Drops a foreign key from the table.\n\t */\n\tasync dropForeignKey(\n\t\ttableOrName: Table | string,\n\t\tforeignKeyOrName: TableForeignKey | string,\n\t): Promise<void> {\n\t\tconst table = InstanceChecker.isTable(tableOrName)\n\t\t\t? tableOrName\n\t\t\t: await this.getCachedTable(tableOrName);\n\t\tconst foreignKey = InstanceChecker.isTableForeignKey(foreignKeyOrName)\n\t\t\t? foreignKeyOrName\n\t\t\t: table.foreignKeys.find((fk) => fk.name === foreignKeyOrName);\n\t\tif (!foreignKey)\n\t\t\tthrow new TypeORMError(`Supplied foreign key was not found in table ${table.name}`);\n\n\t\tawait this.dropForeignKeys(tableOrName, [foreignKey]);\n\t}\n\n\t/**\n\t * Drops a foreign keys from the table.\n\t */\n\tasync dropForeignKeys(\n\t\ttableOrName: Table | string,\n\t\tforeignKeys: TableForeignKey[],\n\t): Promise<void> {\n\t\tconst table = InstanceChecker.isTable(tableOrName)\n\t\t\t? tableOrName\n\t\t\t: await this.getCachedTable(tableOrName);\n\n\t\t// clone original table and remove foreign keys from cloned table\n\t\tconst changedTable = table.clone();\n\t\tforeignKeys.forEach((foreignKey) => changedTable.removeForeignKey(foreignKey));","sourceCodeStart":942,"sourceCodeEnd":978,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/driver/sqlite-abstract/AbstractSqliteQueryRunner.ts#L942-L978","documentation":"dropForeignKey() resolves the foreign key by name (or object) against the cached table; if none matches it throws TypeORMError. The foreign key you are trying to drop does not exist in the table definition TypeORM has loaded.","triggerScenarios":"Calling dropForeignKey(table, 'fkName') where 'fkName' is not present in table.foreignKeys of the cached table.","commonSituations":"Re-running a migration that already dropped it, a typo in the FK name (SQLite often auto-generates these), a prior migration renamed/removed it, or stale cached metadata.","solutions":["Resolve the actual FK name from the live schema via getTable().foreignKeys before dropping.","Make the drop idempotent — check existence first.","Re-fetch the table via getTable() if metadata may be stale."],"exampleFix":"// before\nawait queryRunner.dropForeignKey('user', 'FK_user_org');\n\n// after\nconst table = await queryRunner.getTable('user');\nif (table?.foreignKeys.some((fk) => fk.name === 'FK_user_org')) {\n  await queryRunner.dropForeignKey(table!, 'FK_user_org');\n}","handlingStrategy":"validation","validationCode":"const table = await queryRunner.getTable('user');\nif (table?.foreignKeys.some((fk) => fk.name === 'FK_user_org')) {\n  await queryRunner.dropForeignKey(table!, 'FK_user_org');\n}","typeGuard":"const foreignKeyExists = async (qr: QueryRunner, tableName: string, name: string): Promise<boolean> =>\n  Boolean((await qr.getTable(tableName))?.foreignKeys.some((fk) => fk.name === name));","tryCatchPattern":null,"preventionTips":["Resolve the real FK name from the live schema — SQLite may auto-name FKs.","Make FK drops idempotent.","Re-fetch table metadata when stale."],"tags":["database","migration","sqlite","constraints"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}