Menu

1 / 3

Soft Delete vs Archive Table: Which One Keeps Your Foreign Keys Working

DEV Community: postgres·Son Tran·9 days ago
#euzdJl1z
#devtopostgresql#devto#dev#table#products
Long article (2939 words) — no AI summary yet.
Reading 0:00
15s threshold

Disclosure: I build Schemity , a desktop ERD tool - this post is from our blog and uses it for the examples.

TL;DR: Use a deleted_at column on a table other tables point at, such as products or users, and make those foreign keys reference the surrogate id . Use an archive table for rows nothing references, where you want them physically out of the live table. Both fail in ways PostgreSQL will not warn you about: a soft-deleted row still accepts new references, and an archive copy written with SELECT * lands in the wrong columns after a schema change.

A soft delete column and an archive table fail in opposite places, so the choice is made per table by one question: does another table reference these rows? If it does, keep the rows where they are with a deleted_at column and point every foreign key at the surrogate id . If nothing references them, move them to an archive table and keep the live table plain.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.