简体   繁体   English

使用SQLite进行Peewee空中碰撞检测

[英]Peewee mid-air collision detection with SQLite

I have a couple models that I want to update at the same time. 我有几个要同时更新的模型。 First I get their data from the db with a simple: 首先,我很简单地从数据库中获取数据:

s = Store.get(Store.id == store_id)
new_book = Book.get(Book.id == data[book_id'])
old_book = Book.get(Book.id == s.books.id)

The actual schema is irrelevant here. 实际的架构与此处无关。 Then I do some updates to these models and at the end I save all three of them with: 然后,我对这些模型进行了一些更新,最后我将所有三个模型保存为:

s.save()
new_book.save()
old_book.save()

The function that handles these operations uses the @db.atomic() decorator so the writes are bunched into a single transaction. 处理这些操作的函数使用@db.atomic()装饰器,因此将写入操作@db.atomic()为一个事务。 The problem is that what if, between the point where I get() the data from the DB and the point where I save the modified data, another process changed something with these models in the DB already. 问题是,如果在我从数据库get()数据到保存修改后的数据之间,另一个进程已经在数据库中使用这些模型进行了某些更改,该怎么办? Is there a way to execute those writes ( .save() operations) only if the underlying DB rows have not been changed? 仅在基础数据库行未更改的情况下,才可以执行这些写操作( .save()操作)吗? I could read their last_changed value but again, is there a way to do this and update at the same time? 我可以读取它们的last_changed值,但是有没有办法做到这一点并同时更新? And if data has been changed, simply throw an exception? 如果数据已更改,只需抛出异常?

事实证明,在官方文档中有一个名为Optimistic Locking的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM