Created
March 14, 2013 17:31
-
-
Save garrettr/5163337 to your computer and use it in GitHub Desktop.
Joined table inheritance with SQLAlchemy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask.ext.sqlalchemy import SQLAlchemy | |
app = Flask(__name__) | |
app.config.from_pyfile('settings.cfg') | |
db = SQLAlchemy(app) | |
class User(db.Model): | |
__tablename__ = 'users' | |
id = db.Column(db.Integer, primary_key=True) | |
type = db.Column(db.String(50)) # discriminator | |
username = db.Column(db.String(80), unique=True) | |
password = db.Column(db.String(255), unique=True) | |
is_superuser = db.Column(db.Boolean()) | |
__mapper_args__ = { | |
'polymorphic_identity':'user', | |
'polymorphic_on':type | |
} | |
def __repr__(self): | |
return '<User %r>' % self.username | |
class Source(User): | |
__tablename__ = 'sources' | |
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) | |
__mapper_args__ = { | |
'polymorphic_identity':'source', | |
} | |
def __repr__(self): | |
return '<Source %r>' % self.username | |
class Organization(User): | |
__tablename__ = 'organizations' | |
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) | |
__mapper_args__ = { | |
'polymorphic_identity':'organization', | |
} | |
def __repr__(self): | |
return '<Organization %r>' % self.username | |
class Contact(User): | |
__tablename__ = 'contacts' | |
id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) | |
__mapper_args__ = { | |
'polymorphic_identity':'contact', | |
} | |
def __repr__(self): | |
return '<Contact %r>' % self.username |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment