Coaster provides a number of mixin classes for SQLAlchemy models. To use in your Flask app:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from coaster.sqlalchemy import BaseMixin
app = Flask(__name__)
db = SQLAlchemy(app)
class MyModel(BaseMixin, db.Model):
__tablename__ = 'my_model'
Mixin classes must always appear before db.Model in your model’s base classes.
Provides the id primary key column
Database identity for this model, used for foreign key references from other models
Provides the created_at and updated_at audit timestamps
Timestamp for when this instance was created, in UTC
Timestamp for when this instance was last updated (via the app), in UTC
Provides the permissions() method used by BaseMixin and derived classes
Provides a placeholder url_for() method used by BaseMixin-derived classes
Base mixin class for all tables that adds id and timestamp columns and includes stub permissions() and url_for() methods
Base mixin class for named objects
Autogenerates a name from the title. If the auto-generated name is already in use in this model, make_name() tries again by suffixing numbers starting with 1 until an available name is found.
Base mixin class for named objects within containers. When using this, you must provide an model-level attribute “parent” that is a synonym for the parent object. You must also create a unique constraint on ‘name’ in combination with the parent foreign key. Sample use case in Flask:
class Event(BaseScopedNameMixin, db.Model):
__tablename__ = 'event'
organizer_id = db.Column(db.Integer, db.ForeignKey('organizer.id'))
organizer = db.relationship(Organizer)
parent = db.synonym('organizer')
__table_args__ = (db.UniqueConstraint('name', 'organizer_id'),)
Autogenerates a name from the title. If the auto-generated name is already in use in this model, make_name() tries again by suffixing numbers starting with 1 until an available name is found.
Permissions for this model, plus permissions inherited from the parent.
Generates an abbreviated title by subtracting the parent’s title from this instance’s title.
Base mixin class for named objects with an id tag.
The attribute containing id numbers used in the URL in id-name syntax, for external reference
Base mixin class for objects with an id that is unique within a parent. Implementations must provide a ‘parent’ attribute that is either a relationship or a synonym to a relationship referring to the parent object, and must declare a unique constraint between url_id and the parent. Sample use case in Flask:
class Issue(BaseScopedIdMixin, db.Model):
__tablename__ = 'issue'
event_id = db.Column(Integer, db.ForeignKey('event.id'))
event = db.relationship(Event)
parent = db.synonym('event')
__table_args__ = (db.UniqueConstraint('url_id', 'event_id'),)
Permissions for this model, plus permissions inherited from the parent.
The attribute containing the url id value, for external reference
Base mixin class for named objects with an id tag that is unique within a parent. Implementations must provide a ‘parent’ attribute that is a synonym to the parent relationship, and must declare a unique constraint between url_id and the parent. Sample use case in Flask:
class Event(BaseScopedIdNameMixin, db.Model):
__tablename__ = 'event'
organizer_id = db.Column(db.Integer, db.ForeignKey('organizer.id'))
organizer = db.relationship(Organizer)
parent = db.synonym('organizer')
__table_args__ = (db.UniqueConstraint('url_id', 'organizer_id'),)