V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tanywei
V2EX  ›  Python

请教 SQLAlchemy 继承问题

  •  
  •   tanywei · 2016-10-12 16:14:32 +08:00 · 2059 次点击
    这是一个创建于 2751 天前的主题,其中的信息可能已经有所发展或是发生改变。
    class Employee(Base):
        __tablename__ = 'employee'
        id = Column(Integer, primary_key=True)
        name = Column(String(50))
        type = Column(String(50))
    
        __mapper_args__ = {
            'polymorphic_identity':'employee',
            'polymorphic_on':type
        }
        
    class Engineer(Employee):
        __tablename__ = 'engineer'
        id = Column(Integer, ForeignKey('employee.id'), primary_key=True)
        engineer_name = Column(String(30))
    
        __mapper_args__ = {
            'polymorphic_identity':'engineer',
        }
    
    class Manager(Employee):
        __tablename__ = 'manager'
        id = Column(Integer, ForeignKey('employee.id'), primary_key=True)
        manager_name = Column(String(30))
    
        __mapper_args__ = {
            'polymorphic_identity':'manager',
        }    
    

    如上有 员工、工程师、管理者 3 个表。

    e = Employee.query.filter(xxx=xxx).first()

    我想给 e 加上 Manager 和 Engineer 的角色应该如何表达?

    就是一个员工可以有多种角色。

    4 条回复    2016-11-24 09:58:15 +08:00
    qwer1234asdf
        1
    qwer1234asdf  
       2016-11-23 17:24:27 +08:00
    表本身就建的有问题
    weyou
        2
    weyou  
       2016-11-23 18:06:54 +08:00
    就不该拆表,加一个 role 字段,然后关联到 role 表
    ryd994
        3
    ryd994  
       2016-11-24 04:17:35 +08:00 via Android
    学好范式………
    tanywei
        4
    tanywei  
    OP
       2016-11-24 09:58:15 +08:00
    看官方文档想到的而已,谢谢大家。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5581 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 03:35 · PVG 11:35 · LAX 20:35 · JFK 23:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.