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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: Why use an ORM?

6 点作者 thatxliner大约 2 年前
Why do people use an ORM (such as SQLAlchemy)?<p>I find just writing&#x2F;executing the SQL queries with some sort of api (e.g. asyncpg or sqlite3 for Python) much simpler. The only use-case I can think of is easy of transferring between databases (e.g. from SQLite to PostgreSQL) or a specified schema to reference from (so you get type hints and stuff like that).

3 条评论

colesantiago大约 2 年前
We had a post mortem the other day with and engineering manager and some engineers in my team about a really bad security breach (SQL Injection) and this was due to hand coded SQL in a complex query.<p>From then on our team we decided to switch to using an ORM rather than doing messy SQL queries that could lead to that incident.<p>We are currently still hitting hundreds of thousands of queries on the backend and the team not looked back since.
评论 #35215633 未加载
gregjor大约 2 年前
I can write SQL safely so I’ve never used an ORM myself. I have maintained code that used an ORM.<p>When I’ve asked other programmers why they use an ORM the answer usually comes down to “Because I don’t know SQL well enough to write it myself.”<p>Understanding SQL and the relational model has for me proved the most valuable and durable skill I ever acquired. I’ve avoided rounds of layoffs and got hired just because of that skill.
duskwuff大约 2 年前
Because, in most applications, a lot of your SQL queries will be doing boring CRUD operations (select row by PK, select rows matching indexed condition with limit&#x2F;offset, update&#x2F;delete row by PK, etc). Using an ORM means you don&#x27;t have to waste time writing out boilerplate SQL for those operations. If it&#x27;s a really good ORM, it can help generate more complex queries as well.