我最近在使用python orm peewee時遇到了交易問題。我使用這個orm保存了兩個book實例,並且在兩個節省之間我引發了一個異常,所以我除了沒有將它們保存到數據庫,但它不起作用。誰能解釋爲什麼?我是python的新手,謝謝。Peewee交易似乎不起作用
這個代碼如下:
from peewee import *
def get_db():
return SqliteDatabase("test.db")
class Book(Model):
id = PrimaryKeyField()
name = CharField()
class Meta:
database = get_db()
def test_transaction():
book1 = Book(name="book1")
book2 = Book(name="book2")
db = get_db()
db.create_tables([Book], safe=True)
try:
with db.transaction() as tran:
book1.save()
raise ProgrammingError("test")
book2.save()
except:
pass
for book in Book.select():
print(book.name)
if __name__ == '__main__':
test_transaction()
錯字,我期待(我的英文很差,:))。此外,輸出是book1,但預期的結果是沒有輸出。 –