Replies: 1 comment
-
@longbui99 Unless an exception occurs, the transaction should automatically commit when you exit the context manager. If an exception occurs, the transaction will automatically rollback. The only place I found about commit on transaction is here. I think both Example from docsfrom tortoise import Tortoise, fields, run_async
from tortoise.exceptions import OperationalError
from tortoise.models import Model
from tortoise.transactions import atomic, in_transaction
class Event(Model):
id = fields.IntField(primary_key=True)
name = fields.TextField()
class Meta:
table = "event"
def __str__(self):
return self.name
async def run():
await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
try:
async with in_transaction() as connection:
event = Event(name="Test")
await event.save(using_db=connection)
await Event.filter(id=event.id).using_db(connection).update(
name="Updated name"
)
# manual commit does not change behavior
# await connection.commit()
saved_event = (
await Event.filter(name="Updated name").using_db(connection).first()
)
except OperationalError:
pass
saved_event = await Event.filter(name="Updated name").first()
print(saved_event) # print "Updated name"
if __name__ == "__main__":
run_async(run()) Sorry if I missed your point. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello
I've spent a couple of time but still cannot find when will the tortoise ORM commit the transaction.
Does anyone here can support me or share the reference document.
Thank you very much.
Beta Was this translation helpful? Give feedback.
All reactions