SQLAlchemy patterns

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.

class coaster.sqlalchemy.IdMixin[source]

Provides the id primary key column

id = Column(None, Integer(), table=None, primary_key=True, nullable=False)

Database identity for this model, used for foreign key references from other models

class coaster.sqlalchemy.TimestampMixin[source]

Provides the created_at and updated_at audit timestamps

created_at = Column(None, DateTime(), table=None, nullable=False, default=ColumnDefault(<sqlalchemy.sql.functions.now at 0x10cafb350; now>))

Timestamp for when this instance was created, in UTC

updated_at = Column(None, DateTime(), table=None, nullable=False, onupdate=ColumnDefault(<function <lambda> at 0x10caf7938>), default=ColumnDefault(<sqlalchemy.sql.functions.now at 0x10cafb690; now>))

Timestamp for when this instance was last updated (via the app), in UTC

class coaster.sqlalchemy.PermissionMixin[source]

Provides the permissions() method used by BaseMixin and derived classes

permissions(user, inherited=None)[source]

Return permissions available to the given user on this object

class coaster.sqlalchemy.UrlForMixin[source]

Provides a placeholder url_for() method used by BaseMixin-derived classes

url_for(action='view', **kwargs)[source]

Return public URL to this instance for a given action (default ‘view’)

class coaster.sqlalchemy.BaseMixin[source]

Base mixin class for all tables that adds id and timestamp columns and includes stub permissions() and url_for() methods

class coaster.sqlalchemy.BaseNameMixin(*args, **kw)[source]

Base mixin class for named objects

make_name()[source]

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.

class coaster.sqlalchemy.BaseScopedNameMixin(*args, **kw)[source]

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'),)
make_name()[source]

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(user, inherited=None)[source]

Permissions for this model, plus permissions inherited from the parent.

short_title()

Generates an abbreviated title by subtracting the parent’s title from this instance’s title.

class coaster.sqlalchemy.BaseIdNameMixin(*args, **kw)[source]

Base mixin class for named objects with an id tag.

make_name()[source]

Autogenerates a name from the title

url_id[source]

Return the URL id

url_id_attr = 'id'

The attribute containing id numbers used in the URL in id-name syntax, for external reference

url_name[source]

Returns a URL name combining url_id and name in id-name syntax

class coaster.sqlalchemy.BaseScopedIdMixin(*args, **kw)[source]

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'),)
make_id()[source]

Create a new URL id that is unique to the parent container

permissions(user, inherited=None)[source]

Permissions for this model, plus permissions inherited from the parent.

url_id_attr = 'url_id'

The attribute containing the url id value, for external reference

class coaster.sqlalchemy.BaseScopedIdNameMixin(*args, **kw)[source]

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'),)
make_name()[source]

Autogenerates a title from the name

url_name[source]

Returns a URL name combining url_id and name in id-name syntax

class coaster.sqlalchemy.JsonDict(*args, **kwargs)

Represents a JSON data structure. Usage:

column = Column(JsonDict)
impl

alias of TEXT

Related Topics

This Page