문제

From the official Django docs: https://docs.djangoproject.com/en/1.5/topics/db/transactions/#transaction-rollback

a.save() # Succeeds, but may be undone by transaction rollback
try:
    b.save() # Could throw exception
except IntegrityError:
    transaction.rollback()
c.save() # Succeeds, but a.save() may have been undone

Calling transaction.rollback() rolls back the entire transaction. Any uncommitted database operations will be lost. In this example, the changes made by a.save() would be lost, even though that operation raised no error itself.

Doesn't save do a commit? Then it should not be affected by the rollback or am I wrong?

(It is possible to set the parameter commit=False for the .save() function. That indicates that usually save does a commit)

도움이 되었습니까?

해결책

You're confusing two meanings of the word "commit".

Calling save - on a model or a form - does cause an update to the database, so in that sense it is committed to the database. However, all database operations take place inside a transaction: and those transactions can be committed or rolled back as a block. So the documentation is correct: even if you call save(commit=True), that's still part of an overall transaction that can be rolled back.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top