| 1 |
|
| 2 |
from sqlalchemy import Table, Column, Integer, Unicode, ForeignKey, Index |
| 3 |
from sqlalchemy.ext.declarative import declarative_base |
| 4 |
from sqlalchemy.orm import column_property, relation, mapper |
| 5 |
from sqlalchemy.sql import select |
| 6 |
|
| 7 |
from sadisplay.describe import SQLALCHEMY_VERSION |
| 8 |
|
| 9 |
BASE = declarative_base() |
| 10 |
|
| 11 |
|
| 12 |
class User(BASE): |
| 13 |
__tablename__ = 'user_table' |
| 14 |
|
| 15 |
id = Column(Integer, primary_key=True) |
| 16 |
name = Column(Unicode(50), unique=True) |
| 17 |
|
| 18 |
def login(self): |
| 19 |
pass |
| 20 |
|
| 21 |
def __repr__(self): |
| 22 |
pass |
| 23 |
|
| 24 |
|
| 25 |
class Admin(User): |
| 26 |
__mapper_args__ = {'polymorphic_identity': 'admin'} |
| 27 |
|
| 28 |
phone = Column(Unicode(50)) |
| 29 |
|
| 30 |
def permissions(self): |
| 31 |
pass |
| 32 |
|
| 33 |
def __unicode__(self): |
| 34 |
pass |
| 35 |
|
| 36 |
|
| 37 |
class Manager(User): |
| 38 |
__mapper_args__ = {'polymorphic_identity': 'manager'} |
| 39 |
|
| 40 |
department = Column(Unicode(50)) |
| 41 |
|
| 42 |
def permissions(self): |
| 43 |
pass |
| 44 |
|
| 45 |
def __unicode__(self): |
| 46 |
pass |
| 47 |
|
| 48 |
|
| 49 |
Index('ix_username_department', User.name, Manager.department) |
| 50 |
|
| 51 |
|
| 52 |
class Employee(User): |
| 53 |
__mapper_args__ = {'polymorphic_identity': 'employee'} |
| 54 |
|
| 55 |
manager_id = Column(Integer, ForeignKey(Manager.id)) |
| 56 |
|
| 57 |
|
| 58 |
rank = column_property(Column(Unicode(50)), active_history=True) |
| 59 |
|
| 60 |
|
| 61 |
department = column_property( |
| 62 |
select([Manager.department]).where(Manager.id == manager_id)) |
| 63 |
|
| 64 |
|
| 65 |
class Address(BASE): |
| 66 |
__tablename__ = 'address_table' |
| 67 |
|
| 68 |
id = Column(Integer, primary_key=True) |
| 69 |
user_id = Column(Integer, ForeignKey('user_table.id')) |
| 70 |
user = relation(User, backref="address") |
| 71 |
|
| 72 |
|
| 73 |
books = Table( |
| 74 |
'books', |
| 75 |
BASE.metadata, |
| 76 |
Column('id', Integer, primary_key=True), |
| 77 |
Column('title', Unicode(50), nullable=False, index=True), |
| 78 |
Column('user_id', Integer, ForeignKey('user_table.id')), ) |
| 79 |
|
| 80 |
|
| 81 |
class Book(object): |
| 82 |
pass |
| 83 |
|
| 84 |
|
| 85 |
mapper(Book, books, {'user': relation(User, backref='books')}) |
| 86 |
|
| 87 |
|
| 88 |
notes = Table( |
| 89 |
'notes', |
| 90 |
BASE.metadata, |
| 91 |
Column('id', Integer, primary_key=True), |
| 92 |
Column('name', Unicode(50), nullable=False, index=True), |
| 93 |
Column('user_id', Integer, ForeignKey('user_table.id')), |
| 94 |
Column('body', Unicode(150), nullable=False, index=True), ) |
| 95 |
|
| 96 |
if SQLALCHEMY_VERSION >= (1, 1): |
| 97 |
|
| 98 |
from sqlalchemy import JSON |
| 99 |
|
| 100 |
class JsonData(BASE): |
| 101 |
__tablename__ = 'json_data_declarative' |
| 102 |
|
| 103 |
id = Column(Integer, primary_key=True) |
| 104 |
data = Column(JSON) |
| 105 |
|
| 106 |
json_data = Table('json_data', BASE.metadata, |
| 107 |
Column('id', Integer, primary_key=True), |
| 108 |
Column( |
| 109 |
'data', |
| 110 |
JSON, )) |