HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Postgres suggests "Truncate table ... at the same time"

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
samethepostgressuggeststimetruncatetable

Problem

When running:

TRUNCATE TABLE YYYYY RESTART IDENTITY


I am seeing this error message:

ERROR:  cannot truncate a table referenced in a foreign key constraint
DETAIL:  Table "XXXXX" references "YYYYY".
HINT:  Truncate table "XXXXX" at the same time, or use TRUNCATE ... CASCADE.


Of the two suggestions in HINT:

-
Using TRUNCATE ... CASCADE makes sense, and works, but is less explicit because one must inspect YYYYY to see where the cascade goes. That makes me want to try the other option:

-
Truncate table "XXXXX" at the same time, but my question:

What does it mean to truncate a table at the same time?

I tried adding a TRUNCATE XXXXX... (and wrapping them both in a BEGIN / COMMIT), but that yields the same error.

Solution


  • What does it mean to truncate a table at the same time?




It means with the same statement. You can truncate more than one tables:

TRUNCATE xxxxx, yyyyy RESTART IDENTITY ;


More details in Postgres docs: TRUNCATE.

Code Snippets

TRUNCATE xxxxx, yyyyy RESTART IDENTITY ;

Context

StackExchange Database Administrators Q#182130, answer score: 28

Revisions (0)

No revisions yet.