Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Is it possible to use mongodb with sqlalchemy?


Asked by Maxine Hinton on Dec 12, 2021 MongoDB



Using both SQLAlchemy and MongoDB ¶. TurboGears since version 2.3.8 allows to enable both Ming and SQLAlchemy into the same project. This can be achieved by specifying both the use_ming=True and use_sqlalchemy=True options in configuration. By Default the SQLAlchemy session is considered the primary and is installed as config ['DBSession'] unless it's explicitly set in configuration.
Consequently,
This solves the issue of the module error, however, we still have an error in connection because we want sqlalchemy to connect using the MySQL connector instead of trying to connect directly. This means we have to change the SQLALCHEMY_DATABASE_URI in our python script
In addition, Within the realm of SQLAlchemy, the two databases have a small number of syntactical and behavioral differences that SQLAlchemy accommodates automatically. To connect to a MariaDB database, no changes to the database URL are required:
In fact,
SQLAlchemy methods like .begin (), .commit () and .rollback () pass silently and have no effect. Instead, each statement invoked upon the connection will commit any changes automatically; it sometimes also means that the connection itself will use fewer server-side database resources.
Subsequently,
session.close () will give the connection back to the connection pool of Engine and doesn’t close the connection. engine.dispose () will close all connections of the connection pool. Engine will not use connection pool if you set poolclass=NullPool. So the connection (SQLAlchemy session) will close directly after session.close ().