-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
59 lines (40 loc) · 1.72 KB
/
models.py
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
from sqlalchemy import *
from sqlalchemy.orm import (scoped_session, sessionmaker, relationship,
backref)
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///db.sqlite3', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
class Department(Base):
__tablename__ = 'department'
id = Column(Integer, primary_key=True)
name = Column(String)
class Role(Base):
__tablename__ = 'roles'
id = Column(Integer, primary_key=True)
name = Column(String)
class Employee(Base):
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
__tablename__ = 'employee'
id = Column(Integer, primary_key=True)
name = Column(String)
hired_on = Column(DateTime, default=func.now())
sex = Column(String)
department_id = Column(Integer, ForeignKey('department.id'))
role_id = Column(Integer, ForeignKey('roles.id'))
department = relationship(Department, foreign_keys=[department_id])
role = relationship(Role, foreign_keys=[role_id])
class Form(Base):
__tablename__ = 'form'
id = Column(Integer, primary_key=True)
anonymous = Column(Boolean)
statement = Column(String)
created_at = Column(DateTime, default=func.now())
created_by_id = Column(Integer, ForeignKey('employee.id'))
against_id = Column(Integer, ForeignKey('employee.id'))
created_by = relationship(Employee, foreign_keys=[created_by_id])
against = relationship(Employee, foreign_keys=[against_id])