TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Do You Use Foreign Keys?

4 点作者 ralfhn4 个月前
The answer should be obvious, but isn&#x27;t.<p>I&#x27;m curious to hear real-world experiences that go beyond database design 101 textbooks. Have you faced any tradeoffs, surprises, or regrets around this decision?

3 条评论

Terr_4 个月前
So just tangentially-related to foreign-key usage?<p>Here&#x27;s one: Even if it looks convenient, ON DELETE CASCADE is often not what you want. Customers usually need something much more complicated, so you&#x27;ll be writing external remove-these-before-those loops and steps anyway.<p>For example, a platform will never <i>really</i> want to wipe out the entire subgraph of records for a Customer or Product. Instead they&#x27;ll want to &quot;temporarily deactivate&quot; the account, or at most &quot;mark as permanently disabled, erase <i>some</i> sensitive or bulky information, and insert some audit&#x2F;reason medatata.&quot;<p>Exceptions might be where the related-tables are purely a technical implementation detail, and don&#x27;t represent domain objects the business thinks or cares about.
ddgflorida4 个月前
I&#x27;ve never seen a database with FKs that I regretted were there but plenty without FKs that caused issues. Also - generally make sure you index your FKs.
markus_zhang4 个月前
Worked in OLAP data warehousing but we never enforced a FK in my 7 years of experience involving 3 companies :P