TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: SQL ORM with nested atomic updates?

1 pointsby btdmasterabout 2 months ago
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 comments

jayknightabout 2 months ago
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 未加载
btdmasterabout 2 months ago
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].
maltegabout 2 months ago
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 未加载