records
records copied to clipboard
t.commit generates an error in version 0.5.3
In the Features section of the documentation for version 0.5.3, it says
"Transactions: t = Database.transaction(); t.commit()
."
However, I get an error when trying to do this:
>>> url = '...'
>>> db = records.Database(url)
>>> t = db.transaction()
>>> t.commit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_GeneratorContextManager' object has no attribute 'commit'
However, if I install version 0.5.2, the above code works.
How is the usage of this feature intended to work in version 0.5.3?
Can confirm, this is happening to me as well.
My code:
db = records.Database(...uri)
tx = db.transaction()
try:
db.query(...query)
db.query(...another_query)
tx.commit()
except:
tx.rollback()
...
AttributeError: '_GeneratorContextManager' object has no attribute 'commit'
...
AttributeError: '_GeneratorContextManager' object has no attribute 'rollback'
Nevermind! I figured it out, my code is wrong.
After looking at records.py
and the unit tests, this is the proper way for using transactions:
db = records.Database(...uri)
conn = db.get_connection()
tx = conn.transaction()
try:
db.query(...query)
db.query(...another_query)
tx.commit()
except:
tx.rollback()
finally:
conn.close()
EDIT: Could we improve the documentation for records?
Many thanks for looking into this, I'll keep the ticket open as a request to then update the documentation to read:
"Transactions: conn = db.get_connection(); t = conn.transaction(); t.commit().
"