{"id":"24d4921aaf3826d0","repo":"typeorm/typeorm","slug":"mysql-does-not-support-clearing-table-with-cascade","errorCode":null,"errorMessage":"MySql does not support clearing table with cascade option","messagePattern":"MySql does not support clearing table with cascade option","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"src/driver/aurora-mysql/AuroraMysqlQueryRunner.ts","lineNumber":2089,"sourceCode":"            this.dropIndex(tableOrName, index, ifExists),\n        )\n        await Promise.all(promises)\n    }\n\n    /**\n     * Clears all table contents.\n     * Note: this operation uses SQL's TRUNCATE query which cannot be reverted in transactions.\n     *\n     * @param tableOrName\n     * @param options\n     * @param options.cascade\n     */\n    async clearTable(\n        tableOrName: Table | string,\n        options?: { cascade?: boolean },\n    ): Promise<void> {\n        if (options?.cascade)\n            throw new TypeORMError(\n                `MySql does not support clearing table with cascade option`,\n            )\n        await this.query(`TRUNCATE TABLE ${this.escapePath(tableOrName)}`)\n    }\n\n    /**\n     * Removes all tables from the currently connected database.\n     * Be careful using this method and avoid using it in production or migrations\n     * (because it can clear all your database).\n     *\n     * @param database\n     */\n    async clearDatabase(database?: string): Promise<void> {\n        const dbName = database ?? this.driver.database\n        if (dbName) {\n            const isDatabaseExist = await this.hasDatabase(dbName)\n            if (!isDatabaseExist) return Promise.resolve()\n        } else {","sourceCodeStart":2071,"sourceCodeEnd":2107,"githubUrl":"https://github.com/typeorm/typeorm/blob/04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96/src/driver/aurora-mysql/AuroraMysqlQueryRunner.ts#L2071-L2107","documentation":"Thrown by AuroraMysqlQueryRunner.clearTable when options.cascade is truthy. Aurora MySQL's TRUNCATE (which this runner uses for clearTable) cannot take a CASCADE argument the way Postgres's TRUNCATE ... CASCADE can, so passing cascade:true is rejected rather than silently ignored.","triggerScenarios":"Calling queryRunner.clearTable('users', { cascade: true }) on an aurora-mysql connection; reusable test-cleanup helpers that always pass { cascade: true } across engines.","commonSituations":"Cross-engine test suites where cleanup helpers are shared; refactoring from Postgres to Aurora MySQL without adjusting clear options; CI fixtures assuming cascade semantics.","solutions":["Call clearTable without the cascade option (omit it or set cascade:false).","If FK rows must go too, disable foreign-key checks or delete child rows manually before truncating.","Factor engine-specific clear logic behind a driver-type check in your test helpers."],"exampleFix":"// before\nawait queryRunner.clearTable('users', { cascade: true })\n\n// after\nawait queryRunner.clearTable('users')\n// or, to honor cascade intent, drop children first:\nawait queryRunner.query('SET FOREIGN_KEY_CHECKS = 0')\nawait queryRunner.clearTable('children')\nawait queryRunner.clearTable('users')\nawait queryRunner.query('SET FOREIGN_KEY_CHECKS = 1')","handlingStrategy":"validation","validationCode":"// Only pass cascade on engines that support it\nconst opts = dataSource.options.type === 'aurora-mysql' ? undefined : { cascade: true }\nawait queryRunner.clearTable(table, opts)","typeGuard":"const supportsClearCascade = (t: string): boolean => t === 'postgres' || t === 'aurora-postgres'","tryCatchPattern":null,"preventionTips":["Engine-condition clear options in shared test helpers.","Truncate child tables first or disable FK checks to emulate cascade on MySQL.","Keep teardown helpers per-dialect."],"tags":["aurora-mysql","truncate","schema","dialect-unsupported","typeorm"],"analyzedSha":"04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96","analyzedAt":"2026-08-03T18:27:32.281Z","schemaVersion":2}