zask.ext.sqlalchemy package

Module contents


Adds basic SQLAlchemy support to your application. I have not add all the feature, bacause zask is not for web, The other reason is i can’t handle all the features right now :P

Differents between Flask-SQLAlchemy:

  1. No default scopefunc it means that you need define how to separate sessions your self
  2. No signal session
  3. No query record
  4. No pagination and HTTP headers, e.g. get_or_404
  5. No difference between app bound and not bound
  1. 2015 by the J5.

BSD, see LICENSE for more details.

  1. 2012 by Armin Ronacher, Daniel Neuhäuser.

BSD, see LICENSE for more details.

class zask.ext.sqlalchemy.BindSession(db, autocommit=False, autoflush=True, **options)[source]

Bases: sqlalchemy.orm.session.Session

The BindSession is the default session that Zask-SQLAlchemy uses. It extends the default session system with bind selection. If you want to use a different session you can override the SQLAlchemy.create_session() function.

app = None

The application that this session belongs to.

get_bind(mapper, clause=None)[source]
class zask.ext.sqlalchemy.Model[source]

Bases: object

Baseclass for custom user models.

query = None

an instance of query_class. Can be used to query the database for instances of this model.


the query class used. The query attribute is an instance of this class. By default a orm.Query is used.

alias of Query

class zask.ext.sqlalchemy.SQLAlchemy(app=None, use_native_unicode=True, session_options=None)[source]

Bases: object

This class is used to control the SQLAlchemy integration to one or more Zask applications.

There are two usage modes which work very similarly. One is binding the instance to a very specific Zask application:

app = Zask(__name__)
db = SQLAlchemy(app)

The second possibility is to create the object once and configure the application later to support it:

db = SQLAlchemy()
def create_app():
    app = Zask(__name__)
    return app

This class also provides access to all the SQLAlchemy functions and classes from the sqlalchemy and sqlalchemy.orm modules. So you can declare models like this:

class User(db.Model):
    username = db.Column(db.String(80), unique=True)
    pw_hash = db.Column(db.String(80))
apply_driver_hacks(app, info, options)[source]

This method is called before engine creation and used to inject driver specific hacks into the options. The options parameter is a dictionary of keyword arguments that will then be used to call the sqlalchemy.create_engine() function.

The default implementation provides some saner defaults for things like pool sizes for MySQL and sqlite. Also it injects the setting of SQLALCHEMY_NATIVE_UNICODE.

apply_pool_defaults(app, options)[source]
create_all(bind='__all__', app=None)[source]

Creates all tables.


Helper factory method that creates a scoped session. It internally calls create_session().


Creates the session. The default implementation returns a BindSession.

drop_all(bind='__all__', app=None)[source]

Drops all tables.


Gives access to the engine. If the database configuration is bound to a specific application (initialized with an application) this will always return a database connection. If however the current application is used this might raise a RuntimeError if no application is active at the moment.


Helper method that implements the logic to look up an application.


Returns a dictionary with a table->engine mapping.

This is suitable for use of sessionmaker(binds=db.get_binds(app)).

get_engine(app, bind=None)[source]

Returns a specific engine.


Returns a list of all tables relevant for a bind.


This callback can be used to initialize an application for the use with this database setup. Never use a database in the context of an application not initialized that way or connections will leak.

make_connector(app, bind=None)[source]

Creates the connector for a given state and bind.


Creates the declarative base.


Returns the metadata

reflect(bind='__all__', app=None)[source]

Reflects tables from the database.

class zask.ext.sqlalchemy.SessionMiddleware(db)[source]

Bases: object

server_after_exec(request_event, reply_event)[source]
server_inspect_exception(request_event, reply_event, task_context, exc_infos)[source]

Gets the state for the application