Creating Declarative Base Classes for SQLAlchemy

graphalchemy.sqlmodels.create_base_classes(NodeClass, EdgeClass, [NodeTable = None, [EdgeTable = None, [declared_attr, [Column, [Integer, [Unicode, [Float, [Boolean, [ForeignKey, [relationship, [backref, [Base = None,)[source]

creates base classes (BaseEdge and BaseNode) for use as mixins for graph nodes and edges. ALL parameters must be strings convertible to unicode! Classes need to be subclassed/composited with a declarative_base class

param NodeTable:
 the table for node (unicode)
param NodeClass:
 the class for node (unicode)
param EdgeTable:
 the table for edge (unicode)
param EdgeClass:
 the class for edge (unicode)
param Base:(optional) if a Base is passed, it will be added to the class type for you, thereby requiring no subclassing on your part.
type Base:SQLAlchemy declarative base
Returns:tuple of Node, Edge classes
Return type:(Node, Edge)

NOTE: To overwrite the default inheritance, you can pass in any SQLAlchemy classes used in creating the functions:

declared_attr, Column, Unicode, Integer, Float, Boolean,
relationship, backref, ForeignKey

Creating Base Classes for Flask-SQLAlchemy

graphalchemy.sqlmodels.create_flask_classes(db, NodeClass, EdgeClass, NodeTable=None, EdgeTable=None)[source]

Convenience method for creating Node and Edge base classes for use with Flask-SQLAlchemy. Has nearly the same signature as create_base_classes() But does not take in any overriding methods. Only NodeClass and EdgeClass are required.

The one required parameter is db, which you must create first from the sqlalchemy directions. Example usage:

>>> from flask import Flask
>>> from flask.ext.sqlalchemy import SQLAlchemy
>>> from graphalchemy.sqlmodelss import create_flask_classes
>>> app = Flask(__name__)
>>> app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
>>> db = SQLAlchemy(app)
>>> Node, Edge = create_flask_classes(db, "Node", "Edge")

At this point, you can subclass Node and Edge to add additional traits; however, both Node and Edge will already be subclasses of db.Model, so you don’t need to mix that in.

Or you can just start up your database with:

>>> db.create_all()

Otherwise, the classes created by create_flask_classes() and create_base_classes() are pretty much the same, except that Flask-SQLAlchemy provides some additional features that can be accessed on the Models.

Other Methods

graphalchemy.sqlmodels.sqlite_connect(dbpath, metadata[, create_engine[, sessionmaker[, echo=True]]])[source]

return an sqllite connection to the given dbpath. Optional arguments default to sqlalchemy functions.


param dbpath:path (relative or absolute) to database (unicode/string) NOT “sqlite://”
param metadata:something that supports create_all() to create/load tables and has a bind attribute
type metadata:should create tables with create_all()
type create_engine:
 function (dbpath) -> engine
param sessionmaker:
 (optional) must take bind=engine, return a class that can be called to create a session
type sessionamker:
 function (bind=engine) –> Session
param event:event creator for engine (from SQLAlchemy)
param bool enforce_fk:
 set database to enforce foreign key relationships
default enforce_fk:


returns:(engine, session)
raises:ValueError if passed a path that does not exist or a non-valid path.

Table Of Contents

Previous topic

Base Classes

Next topic

Notes on integrating GraphAlchemy with web frameworks

This Page