在插入期间,sqlalchemy中未填写polymorphic_on-column

时间:2017-06-28 15:44:22

标签: python sqlalchemy

如果我有这样的层次结构和模型定义:

class Company(Base):
    __tablename__ = "company"

    company_id = Column(Integer, primary_key=True)
    company_name = Column(Text)
    employees = relationship("Employee")


class Employee(Base):
    __tablename__ = "employee"

    employee_id = Column(Integer, primary_key=True)
    company_id = Column(Integer, ForeignKey("company.company_id"))
    position = Column(Text)

    __mapper_args__ = {"polymorphic_on": position, 'polymorphic_identity':'employee'}


class Manager(Employee, ConcreteBase):
    __tablename__ = "manager"

    employee_id = Column(Integer, ForeignKey("employee.employee_id"), primary_key=True)
    fancy_title = Column(Text)

    __mapper_args__ = {"polymorphic_identity": "Manager"}

然后我插入一个Manager,如此:

ceo = Manager(fancy_title="CEO")
big_inc = Company(company_name="Big Inc.", employees=[ceo])
session.add(big_inc)
session.commit()

当我查看Employee - 表时,position字段未填写。的为什么吗

companydb=# SELECT * FROM employee;
 employee_id | company_id | position 
-------------+------------+----------
           1 |          1 | 
(1 row)

我期待它说:

companydb=# SELECT * FROM employee;
 employee_id | company_id | position 
-------------+------------+----------
           1 |          1 | Manager
(1 row)

1 个答案:

答案 0 :(得分:0)

之前多次回答我自己的问题:

class Manager(Employee, ConcreteBase):

不应该有ConcreteBase

class Manager(Employee):

显然因为对孩子这样做没有任何意义......