records icon indicating copy to clipboard operation
records copied to clipboard

t.commit generates an error in version 0.5.3

Open dougmitarotonda opened this issue 5 years ago • 3 comments

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?

dougmitarotonda avatar May 09 '19 16:05 dougmitarotonda

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'

TokenChingy avatar May 15 '19 07:05 TokenChingy

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?

TokenChingy avatar May 15 '19 07:05 TokenChingy

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()."

dougmitarotonda avatar May 17 '19 21:05 dougmitarotonda