1671
MariaDBERRORCommonInnoDBHIGH confidence

Cannot DISCARD TABLESPACE while foreign key checks are running

Production Risk

High — tablespace operations can render the table inaccessible if not handled carefully.

What this means

ALTER TABLE ... DISCARD TABLESPACE cannot be executed while foreign key constraint checking is enabled and the table is referenced by another table.

Why it happens
  1. 1The table is referenced by a foreign key in another table.
  2. 2FOREIGN_KEY_CHECKS = 1 prevents the tablespace discard.
How to reproduce
trigger — this will error
trigger — this will error
ALTER TABLE child_table DISCARD TABLESPACE;
-- parent_table has a FK referencing child_table

expected output

ERROR 1671 (HY000): Cannot discard the tablespace of a table that is referenced by a foreign key constraint.

Fix

Disable foreign key checks before discarding the tablespace

Disable foreign key checks before discarding the tablespace
SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE child_table DISCARD TABLESPACE;
SET FOREIGN_KEY_CHECKS = 1;

Why this works

Disabling FK checks allows the tablespace operation to proceed.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 1671 ER_DISCARD_FK_CHECKS_RUNNING

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors