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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: SQL ORM with nested atomic updates?

1 点作者 btdmaster大约 2 个月前
I have some data structures that look like this:<p>Car:<p><pre><code> Seats: [{ID: frontSeatId}, {ID: backSeatId}] Driver: {&#x27;Name&#x27;:&#x27;Joe&#x27;, &#x27;SalaryUSD&#x27;: 42000} SoundSystem: {&#x27;TypeID&#x27;: androidAutoId, Protocols: [{ID: radioProtocolId}, {ID: bluetoothProtocolId}]} </code></pre> In this system, we often need to do partial updates, like:<p>Car:<p><pre><code> Driver: {&#x27;Name&#x27;: &#x27;Jack&#x27;} SoundSystem: {Protocols: [{ID: bluetoothProtocolId}]} </code></pre> Meaning that the SoundSystemProtocols table should delete and create new entries such that the car only supports bluetooth, and the driver should get renamed from Joe to Jack.<p>Is there an ORM that lets you do these partial updates atomically, without writing custom controllers for each Car-like object?<p>I want ergonomics over performance. I don&#x27;t mind the language. Ideally it should allow a simple interface where the model defines the controller.<p>I&#x27;ve tried SQLModel, which promises this, but ended up with confusing JSON validation, mixing between SQLModel types and the internal SQLAlchemy model, and still had to write session.add(seats) by hand anyway.<p>If this doesn&#x27;t exist for SQL, does another database support ID-as-value like this, where passing in an ID changes a reference, and passing in a value changes the value at that reference?

3 条评论

jayknight大约 2 个月前
Does this do what you are looking for?<p><a href="https:&#x2F;&#x2F;docs.sqlalchemy.org&#x2F;en&#x2F;20&#x2F;orm&#x2F;session_transaction.html#using-savepoint" rel="nofollow">https:&#x2F;&#x2F;docs.sqlalchemy.org&#x2F;en&#x2F;20&#x2F;orm&#x2F;session_transaction.ht...</a><p>It&#x27;s the backing DBMS that will enforce atomicity. Postgres and sqlite allow the semantics of nested transactions with SAVEPOINTs (I don&#x27;t know about other RDBMSes).
评论 #43464812 未加载
btdmaster大约 2 个月前
I found <a href="https:&#x2F;&#x2F;github.com&#x2F;sqlalchemy&#x2F;sqlalchemy&#x2F;discussions&#x2F;7336#discussioncomment-10751058" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sqlalchemy&#x2F;sqlalchemy&#x2F;discussions&#x2F;7336#di...</a>, then I added list support by checking the type of kw[key].
malteg大约 2 个月前
something like <a href="https:&#x2F;&#x2F;gorm.io&#x2F;docs&#x2F;update.html#Update-from-SubQuery" rel="nofollow">https:&#x2F;&#x2F;gorm.io&#x2F;docs&#x2F;update.html#Update-from-SubQuery</a> ?
评论 #43472862 未加载