typeorm/typeorm · error · TypeORMError

MySql does not support clearing table with cascade option

Error message

MySql does not support clearing table with cascade option

What it means

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.

Source

Thrown at src/driver/aurora-mysql/AuroraMysqlQueryRunner.ts:2089

            this.dropIndex(tableOrName, index, ifExists),
        )
        await Promise.all(promises)
    }

    /**
     * Clears all table contents.
     * Note: this operation uses SQL's TRUNCATE query which cannot be reverted in transactions.
     *
     * @param tableOrName
     * @param options
     * @param options.cascade
     */
    async clearTable(
        tableOrName: Table | string,
        options?: { cascade?: boolean },
    ): Promise<void> {
        if (options?.cascade)
            throw new TypeORMError(
                `MySql does not support clearing table with cascade option`,
            )
        await this.query(`TRUNCATE TABLE ${this.escapePath(tableOrName)}`)
    }

    /**
     * Removes all tables from the currently connected database.
     * Be careful using this method and avoid using it in production or migrations
     * (because it can clear all your database).
     *
     * @param database
     */
    async clearDatabase(database?: string): Promise<void> {
        const dbName = database ?? this.driver.database
        if (dbName) {
            const isDatabaseExist = await this.hasDatabase(dbName)
            if (!isDatabaseExist) return Promise.resolve()
        } else {

View on GitHub (pinned to 04ff4daedc)

Solutions

  1. Call clearTable without the cascade option (omit it or set cascade:false).
  2. If FK rows must go too, disable foreign-key checks or delete child rows manually before truncating.
  3. Factor engine-specific clear logic behind a driver-type check in your test helpers.

Example fix

// before
await queryRunner.clearTable('users', { cascade: true })

// after
await queryRunner.clearTable('users')
// or, to honor cascade intent, drop children first:
await queryRunner.query('SET FOREIGN_KEY_CHECKS = 0')
await queryRunner.clearTable('children')
await queryRunner.clearTable('users')
await queryRunner.query('SET FOREIGN_KEY_CHECKS = 1')
Defensive patterns

Strategy: validation

Validate before calling

// Only pass cascade on engines that support it
const opts = dataSource.options.type === 'aurora-mysql' ? undefined : { cascade: true }
await queryRunner.clearTable(table, opts)

Type guard

const supportsClearCascade = (t: string): boolean => t === 'postgres' || t === 'aurora-postgres'

Prevention

When it happens

Trigger: Calling queryRunner.clearTable('users', { cascade: true }) on an aurora-mysql connection; reusable test-cleanup helpers that always pass { cascade: true } across engines.

Common situations: Cross-engine test suites where cleanup helpers are shared; refactoring from Postgres to Aurora MySQL without adjusting clear options; CI fixtures assuming cascade semantics.

Related errors


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