推荐学习书目
› 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 · Oct 12, 2016 · 3184 views
    This topic created in 3642 days ago, the information mentioned may be changed or developed.
    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 replies  •  2016-11-24 09:58:15 +08:00
    qwer1234asdf
        1
    qwer1234asdf  
       Nov 23, 2016
    表本身就建的有问题
    weyou
        2
    weyou  
       Nov 23, 2016
    就不该拆表,加一个 role 字段,然后关联到 role 表
    ryd994
        3
    ryd994  
       Nov 24, 2016 via Android
    学好范式………
    tanywei
        4
    tanywei  
    OP
       Nov 24, 2016
    看官方文档想到的而已,谢谢大家。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2342 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:16 · PVG 11:16 · LAX 20:16 · JFK 23:16
    ♥ Do have faith in what you're doing.