TRUNCATE SQL Origin: Data OSI layer 7 · Application Last updated 18 Nov 2026

Cannot truncate a table referenced by a FOREIGN KEY constraint

TRUNCATE is refused because another table has a foreign key pointing to it.

Exact message you may see Cannot truncate table because it is being referenced by a FOREIGN KEY constraintCannot truncate a table referenced in a foreign key constraintTRUNCATE referenced by foreign keytruncate table cannot be truncated FOREIGN KEY CASCADEERROR 1701 Cannot truncate a table referenced in a foreign key constraint
Seen on: SQL SQL Server MySQL PostgreSQL

Meaning

Unlike DELETE, TRUNCATE can't run when foreign keys reference the table (SQL Server error 4712, MySQL 1701, PostgreSQL needs CASCADE). Delete child rows first, use TRUNCATE ... CASCADE (PostgreSQL), disable/drop the constraints temporarily, or use DELETE.

Common causes

  • Child table with FK to the table
  • Truncating in the wrong order
  • Constraint not dropped first
  • Trying to reset test data
  • Self-referencing foreign key

⚡ Quick fix

  1. Truncate/delete the child tables first
  2. PostgreSQL: TRUNCATE parent CASCADE
  3. Temporarily drop the FK, truncate, re-add it

Detailed fix by platform

SQL

  1. TRUNCATE TABLE orders, order_items RESTART IDENTITY CASCADE; -- PostgreSQL

How to diagnose

  1. Referencing tables — Which?
  2. Order — Children first?
  3. CASCADE — Acceptable?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

1022 — MySQL ERROR 1022 (23000): Can't write; duplicate key in table (MySQL)10221048 — MySQL ERROR 1048 (23000): Column 'x' cannot be null (MySQL)10481062 — MySQL ERROR 1062 (23000): Duplicate entry for key (MySQL)10621451 — MySQL ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (MySQL)14511452 — MySQL ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (MySQL)145223502 — ERROR: null value in column "x" of relation "y" violates not-null constraint (PostgreSQL)2350223503 — ERROR: insert or update on table "x" violates foreign key constraint "fk" — Key (user_id)=(5) is not present in table "users" (PostgreSQL)2350323503 parent — ERROR: update or delete on table "users" violates foreign key constraint on table "orders" — Key (id)=(5) is still referenced (PostgreSQL)23503 parentTRUNCATE

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.