kennethreitz / records

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

Use close_with_result rather than closing connections too early. (Fixes the sqlite error.) #190

Closed shacknetisp closed 5 months ago

shacknetisp commented 4 years ago

When called from the Database query methods, the Record generator (https://github.com/shacknetisp/records/blob/ab049652ffb11378cd257d751c7e6d557308d841/records.py#L367) accesses the ResultProxy cursor after the database connection has been closed. This causes problems with SQLite in particular (#149).

This PR corrects the behavior by using the close_with_result connection parameter for the query methods, since they only return one result which is used in the generator, instructing SQLAlchemy to have the ResultProxy cursor close the connection itself once all the results (via the generator) have been consumed. In this case we do not close the connection manually.

For more documentation, see https://docs.sqlalchemy.org/en/13/core/connections.html and find "close_with_result".

Fixes #149. Fixes #128. Fixes #189. Closes #177.

joshuarost commented 4 years ago

I encounter the same problem! Applied those changes locally and worked like a charm.