從my previous question開始,我使用MySQL和InnoDB引擎,並建立了一個簡單的測試表。當我運行以下代碼:調用save()後,Django請求事務不會完全回滾。 TransactionMiddleware不像預期的那樣運行
c.execute("""insert into test(text) values('test1')""")
conn.commit()
c.execute("""insert into test(text) values('test2')""")
# Raise an exception to simulate an error
raise Exception('spam', 'eggs')
c.execute("""insert into test(text) values('test3')""")
conn.commit()
我在數據庫中看到一個僅用於test1的條目,如預期的那樣。
建立相關模型類,並在Django中運行等效代碼後,(我在settings.py指定TransactionMiddleware):
def transaction_test(request):
t1 = Test(text='test1')
t2 = Test(text='test2')
t1.save()
# Raise an exception to simulate an error
raise Exception('spam', 'eggs')
t2.save()
return render_to_response('index.html')
然後,我仍然看到了「測試1」的條目在表中,而我會預期整個交易已經回滾,包括'test1'的保存,根據文檔中給出的描述給出的TransactionMiddleware
有沒有其他人看到這個?是否有其他Django設置我缺少?
你在哪裏開始交易? –