Skip to content

Instantly share code, notes, and snippets.

@jennielees
jennielees / gist:b8f6f03e6fa39e9f0f16
Created March 14, 2015 21:56
Flask-SQLAlchemy Overrides

This was going to be an answer to pallets-eco/flask-sqlalchemy#269 but I realised I was getting further away from answering the actual question.

How to make SQLAlchemy models that are reusable, and compatible with Flask-SQLAlchemy

I looked into doing something similar, in order to use SQLAlchemy models outside the app context and to automatically use some mixins for all models created, as well as to override the default table naming scheme.

Worth noting there are some unmerged PRs around overriding the metadata/base: pallets-eco/flask-sqlalchemy#61

The Model and BaseQuery classes are standalone -- it doesn't look like they require the instantiation, so you could import and inherit from them, or rewrite them to support what you need. The issue is then pulling your custom classes back into the SQLAlchemy() part, since the class names are hardcoded.

@typehorror
typehorror / Flask-SQLAlchemy Caching.md
Last active February 15, 2024 14:44
Flask SQLAlchemy Caching

Flask-SQLAlchemy Caching

The following gist is an extract of the article Flask-SQLAlchemy Caching. It allows automated simple cache query and invalidation of cache relations through event among other features.

Usage

retrieve one object

# pulling one User object

user = User.query.get(1)

@catermelon
catermelon / config.py
Created October 4, 2013 15:49
Flask-SQLAlchemy - separating reads and writes
# This is not used unless SQLALCHEMY_BINDS is not present
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://{username}:{password}@{hostname}/{database}?charset=utf8'
SQLALCHEMY_BINDS = {
'master': 'mysql+pymysql://{username}:{password}@{hostname}/{database}?charset=utf8',
'slave': 'mysql+pymysql://{username}:{password}@{hostname}/{database}?charset=utf8'
}