1 from sqlalchemy
import *
3 import migrate
.changeset
# monkeypatches Column
5 from sqlalchemy
import orm
6 from sqlalchemy
.ext
.declarative
import declarative_base
7 TableBase
= declarative_base()
10 class User(TableBase
):
11 __tablename__
= 'users'
12 id = Column(Integer
, primary_key
=True)
15 class Role(TableBase
):
16 __tablename__
= 'roles'
17 id = Column(Integer
, primary_key
=True, nullable
=False)
18 name
= Column(Unicode(64), nullable
=False)
19 icon
= Column(Unicode(64), nullable
=False)
21 class UserRole(TableBase
):
22 __tablename__
= 'user_roles'
23 user_id
= Column(Integer
, ForeignKey('users.id'), primary_key
=True, nullable
=False, autoincrement
=False)
24 role_id
= Column(Integer
, ForeignKey('roles.id'), primary_key
=True, nullable
=False, autoincrement
=False)
26 class RolePermission(TableBase
):
27 __tablename__
= 'role_permissions'
28 id = Column(Integer
, nullable
=False, primary_key
=True)
29 role_id
= Column(Integer
, ForeignKey('roles.id'), nullable
=True)
30 permission
= Column(Unicode(64), nullable
=False)
33 def upgrade(migrate_engine
):
34 TableBase
.metadata
.bind
= migrate_engine
35 Role
.__table__
.create()
36 UserRole
.__table__
.create()
37 RolePermission
.__table__
.create()
39 def downgrade(migrate_engine
):
40 TableBase
.metadata
.bind
= migrate_engine
41 RolePermission
.__table__
.drop()
42 UserRole
.__table__
.drop()