graphene/graphene-sqlalchemy/examples/flask_sqlalchemy/models.py

40 lines
1.2 KiB
Python
Raw Normal View History

from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func
from sqlalchemy.orm import backref, relationship
2016-01-23 08:44:41 +03:00
from database import Base
class Department(Base):
__tablename__ = 'department'
id = Column(Integer, primary_key=True)
name = Column(String)
2016-04-13 15:16:01 +03:00
class Role(Base):
__tablename__ = 'roles'
role_id = Column(Integer, primary_key=True)
name = Column(String)
2016-01-23 08:44:41 +03:00
class Employee(Base):
__tablename__ = 'employee'
id = Column(Integer, primary_key=True)
name = Column(String)
# Use default=func.now() to set the default hiring time
# of an Employee to be the current time when an
# Employee record was created
hired_on = Column(DateTime, default=func.now())
department_id = Column(Integer, ForeignKey('department.id'))
2016-04-13 15:16:01 +03:00
role_id = Column(Integer, ForeignKey('roles.role_id'))
2016-01-23 08:44:41 +03:00
# Use cascade='delete,all' to propagate the deletion of a Department onto its Employees
department = relationship(
Department,
backref=backref('employees',
uselist=True,
cascade='delete,all'))
2016-04-13 15:16:01 +03:00
role = relationship(
Role,
backref=backref('roles',
uselist=True,
cascade='delete,all'))