Back to articles
Soft-deleting Postgres rows without losing the URL slug

Soft-deleting Postgres rows without losing the URL slug

via Dev.toDaniel Rusnok

Six lines of code that let you release a unique-constrained slug back into the namespace without dropping the constraint. I want soft-delete on my blog posts. I also want slug to be UNIQUE NOT NULL . These two requirements quietly fight each other, and the standard fixes — partial unique indexes, deleted_at IS NULL predicates, separate archive tables — are all heavier than what I needed. Here's the six-line trick I shipped instead. The conflict A blog post has a URL slug like my-cool-post . I want UNIQUE on slug so I can serve /blog/my-cool-post without ambiguity. I also want DELETE to be soft — set status = 'archived' so I can recover the post if I change my mind, and so historical analytics don't get orphaned. Now the user re-uses the title "My Cool Post" two months later. They want a fresh post at /blog/my-cool-post . The INSERT fails because the archived row still owns the slug. Get 5 practical emails covering the exact TypeScript concepts that trip up C# developers. drippery.app S

Continue reading on Dev.to

Opens in a new tab

Read Full Article
1 views

Related Articles