问HN:支持嵌套原子更新的SQL ORM?

1作者: btdmaster2 个月前原帖
我有一些数据结构如下所示: 汽车: ``` 座位: [{ID: frontSeatId}, {ID: backSeatId}] 驾驶员: {'姓名':'乔', '薪资美元': 42000} 音响系统: {'类型ID': androidAutoId, 协议: [{ID: radioProtocolId}, {ID: bluetoothProtocolId}]} ``` 在这个系统中,我们经常需要进行部分更新,例如: 汽车: ``` 驾驶员: {'姓名': '杰克'} 音响系统: {协议: [{ID: bluetoothProtocolId}]} ``` 这意味着音响系统协议表应该删除旧条目并创建新条目,以使汽车仅支持蓝牙,同时驾驶员的名字应该从乔改为杰克。 有没有一种ORM可以让你以原子方式进行这些部分更新,而无需为每个类似汽车的对象编写自定义控制器? 我更看重易用性而非性能。语言方面我没有特别的要求。理想情况下,它应该提供一个简单的接口,让模型定义控制器。 我尝试过SQLModel,它承诺可以实现这一点,但最终遇到了令人困惑的JSON验证,SQLModel类型与内部SQLAlchemy模型之间的混淆,最终还是不得不手动写session.add(seats)。 如果SQL中没有这种实现,是否有其他数据库支持像这样的ID作为值的方式,即传入一个ID会改变引用,而传入一个值会改变该引用处的值?
查看原文
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?