Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you determine sqlalchemy driver from the session?

Some libraries and sites (like kotti) expose a database session that is loaded from a configuration file (it uses pyramid).

In general you can ignore the driver for sqlalchemy, but there are a few issues like getting a random row and using timezones with sqlite, which require you to have specific behavior for different engines.

Thing is, I can't see to find out how to determine what driver you're using at run time.

How do you do this?

Specifically, how, from a session (not an engine or a session factory) can you work backwards and figure this out?

like image 279
Doug Avatar asked Oct 25 '13 01:10

Doug


People also ask

How do I get SQLAlchemy connection?

connect() method returns a Connection object, and by using it in a Python context manager (e.g. the with: statement) the Connection. close() method is automatically invoked at the end of the block. The Connection , is a proxy object for an actual DBAPI connection.

How does SQLAlchemy Session work?

The Session begins in a mostly stateless form. Once queries are issued or other objects are persisted with it, it requests a connection resource from an Engine that is associated with the Session , and then establishes a transaction on that connection.

What creates SQLAlchemy Engine?

The Engine is the starting point for any SQLAlchemy application. It's “home base” for the actual database and its DBAPI, delivered to the SQLAlchemy application through a connection pool and a Dialect , which describes how to talk to a specific kind of database/DBAPI combination.

What is Session commit SQLAlchemy?

Session. commit() is used to commit the current transaction. It always issues Session. flush() beforehand to flush any remaining state to the database; this is independent of the “autoflush” setting.


1 Answers

If you do this

session.bind.dialect.name

it will return something like sqlite or mysql, i.e. the part at the beginning of a URL (mysql://...). Most other information can also be fetched from the dialect object if you are interested in more. You can find this on any engine or connection (which bind is).

like image 93
javex Avatar answered Oct 26 '22 23:10

javex