ORM
Object-Relational Mapping is implemented using Flask-SQLAlchemy.
Defining and mapping models
First off you need to define your models as described by the SQL-Alchemy documentation:
# app/database/models/users.py
from edmunds.database.db import db
class User(db.Model):
# __bind_key__ = 'users_database'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
# app/database/models/tags.py
from edmunds.database.db import db
class Tag(db.Model):
# __bind_key__ = 'users_database'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True)
# app/database/models/usertags.py
from edmunds.database.db import db
UserTagsTable = db.Table(
'user_tags',
db.Column('user_id', db.Integer, db.ForeignKey('tags.id'), primary_key=True),
db.Column('tag_id', db.Integer, db.ForeignKey('users.id'), primary_key=True),
# info={'bind_key': 'users_database'}
)
Insert, Update, Delete
The actions work the same way as Flask-SQLAlchemy does:
session = app.database_session()
peter = User(name='peter')
# Inserting
session.add(peter)
session.commit()
print peter.id
# Updating
peter.name = 'peter verkest'
session.commit()
# Deleting
session.delete(peter)
session.commit()
Further documentation:
Note: These actions are handy, but not efficient when running in bulk. Take a look at this StackOverflow-question.
Querying
Querying works as defined in Flask-SQLAlchemy.
users = User.query.all()
peter = User.query.filter_by(name='peter').first()
Further documentation on querying: