gimitec=# TRUNCATE mail_message; ERROR: cannot truncate a table referenced in a foreign key constraint DETAIL: Table "mail_channel_partner" references "mail_message". HINT: Truncate table "mail_channel_partner" at the same time, or use TRUNCATE ... CASCADE.
1 Answer
Just run:
> root@dna:~# su postgres > postgres@dna:/root$ psql could not change directory to "/root": Permission denied psql (14.18 (Ubuntu 14.18-1.pgdg24.04+1)) Type "help" for help. > postgres=# \c base You are now connected to database "base" as user "postgres". base=# TRUNCATE TABLE mail_message CASCADE;
and the log will look like:
NOTICE: truncate cascades to table "mail_channel_partner" NOTICE: truncate cascades to table "mail_compose_message" NOTICE: truncate cascades to table "mail_mail" NOTICE: truncate cascades to table "mail_message_mail_channel_rel" NOTICE: truncate cascades to table "mail_message_res_partner_needaction_rel" NOTICE: truncate cascades to table "mail_message_res_partner_rel" NOTICE: truncate cascades to table "mail_message_res_partner_starred_rel" NOTICE: truncate cascades to table "mail_resend_message" NOTICE: truncate cascades to table "mail_tracking_value" NOTICE: truncate cascades to table "message_attachment_rel" NOTICE: truncate cascades to table "rating_rating" NOTICE: truncate cascades to table "sms_resend" NOTICE: truncate cascades to table "sms_sms" NOTICE: truncate cascades to table "snailmail_letter" NOTICE: truncate cascades to table "snailmail_letter_format_error" NOTICE: truncate cascades to table "account_invoice_send" NOTICE: truncate cascades to table "account_tour_upload_bill" NOTICE: truncate cascades to table "mail_compose_message_ir_attachments_object_rel" NOTICE: truncate cascades to table "mail_compose_message_ir_attachments_rel" NOTICE: truncate cascades to table "mail_compose_message_mailing_list_rel" NOTICE: truncate cascades to table "mail_compose_message_res_partner_rel" NOTICE: truncate cascades to table "mail_mail_res_partner_rel" NOTICE: truncate cascades to table "mail_message_res_partner_needaction_rel_mail_resend_message_rel" NOTICE: truncate cascades to table "mail_resend_partner" NOTICE: truncate cascades to table "mailing_trace" NOTICE: truncate cascades to table "sms_resend_recipient" NOTICE: truncate cascades to table "snailmail_letter_missing_required_fields" NOTICE: truncate cascades to table "account_move_account_invoice_send_rel" NOTICE: truncate cascades to table "link_tracker_click" NOTICE: truncate cascades to table "snailmail_confirm_invoice" TRUNCATE TABLE
> ref from: https://stackoverflow.com/questions/56700466/postgresql-truncate-table-with-foreign-key-constraint
on production systems when work time to time the table "mail_message" will become bigger and bigger. One day you need to truncate this table on PostgreSQL to help saving their life to continued system working.