| 1 |
|
| 2 |
from sqlalchemy import Table, Column, Integer, Unicode, ForeignKey, Index |
| 3 |
from sqlalchemy.ext.declarative import declarative_base |
| 4 |
from sqlalchemy.orm import relation, mapper |
| 5 |
|
| 6 |
BASE = declarative_base() |
| 7 |
|
| 8 |
|
| 9 |
class User(BASE): |
| 10 |
__tablename__ = 'user_table' |
| 11 |
|
| 12 |
id = Column(Integer, primary_key=True) |
| 13 |
name = Column(Unicode(50)) |
| 14 |
|
| 15 |
def login(self): |
| 16 |
pass |
| 17 |
|
| 18 |
def __repr__(self): |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
class Admin(User): |
| 23 |
__tablename__ = 'admin_table' |
| 24 |
__mapper_args__ = {'polymorphic_identity': 'user_table'} |
| 25 |
|
| 26 |
id = Column(Integer, ForeignKey('user_table.id'), primary_key=True) |
| 27 |
phone = Column(Unicode(50)) |
| 28 |
|
| 29 |
def permissions(self): |
| 30 |
pass |
| 31 |
|
| 32 |
def __unicode__(self): |
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
class Address(BASE): |
| 37 |
__tablename__ = 'address_table' |
| 38 |
|
| 39 |
id = Column(Integer, primary_key=True) |
| 40 |
user_id = Column(Integer, ForeignKey('user_table.id')) |
| 41 |
user = relation(User, backref="address") |
| 42 |
|
| 43 |
|
| 44 |
books = Table( |
| 45 |
'books', |
| 46 |
BASE.metadata, |
| 47 |
Column('id', Integer, primary_key=True), |
| 48 |
Column('title', Unicode(200), nullable=False), |
| 49 |
Column('user_id', Integer, ForeignKey('user_table.id')), ) |
| 50 |
|
| 51 |
Index("ix_user_title", books.c.user_id, books.c.title) |
| 52 |
|
| 53 |
|
| 54 |
class Book(object): |
| 55 |
pass |
| 56 |
|
| 57 |
|
| 58 |
mapper(Book, books, {'user': relation(User, backref='books')}) |
| 59 |
|
| 60 |
|
| 61 |
notes = Table( |
| 62 |
'notes', |
| 63 |
BASE.metadata, |
| 64 |
Column('id', Integer, primary_key=True), |
| 65 |
Column('name', Unicode(200), nullable=False), |
| 66 |
Column('user_id', Integer, ForeignKey('user_table.id')), ) |
| 67 |
|
| 68 |
if __name__ == '__main__': |
| 69 |
import codecs |
| 70 |
import sadisplay |
| 71 |
|
| 72 |
desc = sadisplay.describe(globals().values()) |
| 73 |
|
| 74 |
with codecs.open('/tmp/schema.plantuml', 'w', encoding='utf-8') as f: |
| 75 |
f.write(sadisplay.plantuml(desc)) |
| 76 |
|
| 77 |
with codecs.open('/tmp/schema.dot', 'w', encoding='utf-8') as f: |
| 78 |
f.write(sadisplay.dot(desc)) |