T
T
th3mis2015-08-31 15:08:27
SQL
th3mis, 2015-08-31 15:08:27

What is the correct way to use SQL Alchemy?

What is the best way to use SQL Alchemy in this case?
There is a database, in the table various records. In a loop, periodically I receive data and use the returned ORM object in many places, with access methods to the necessary attributes (columns). After the fetch, I make a commit to close the transaction. But after accessing any attribute of the object, a new transaction is apparently opened, which no one closes (do not write to me through each line of session.commit()). As a result, I get an unwanted idle in transaction.
The documentation says that using auto_commit=True is not kosher. As a result, I would like to receive result in a function in the form of a dictionary, like in Django. Or how to correct the code?

class DBInterface(object):
    def __init__(self):
        engine = create_engine("...", encoding='utf8', echo=False)
        Base.metadata.create_all(engine)
        Session = sessionmaker(bind=engine)

    def get_project_info(self, project):
        query = self.session.query(Projects)
        query = query.filter(Projects.name == project).all()
        if len(query) > 1:
            raise LookupError('...')
        result = query[0]
        return result

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question