kennethreitz / records

SQL for Humans™
https://pypi.python.org/pypi/records/
ISC License
7.14k stars 570 forks source link

t.commit generates an error in version 0.5.3 #178

Open dougmitarotonda opened 5 years ago

dougmitarotonda commented 5 years ago

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?

TokenChingy commented 5 years ago

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 commented 5 years ago

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?

dougmitarotonda commented 5 years ago

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