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