使用父构造函数

时间:2015-05-28 23:03:44

标签: python sqlalchemy

我有http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#joined-table-inheritance

中列出的类继承方案
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()


class Parent(Base):
    __tablename__ = 'parent'

    id = Column(Integer, primary_key=True)
    type = Column(String)

    __mapper_args__ = {'polymorphic_on': type}


class Child(Parent):
    __tablename__ = 'child'

    id = Column(Integer, ForeignKey('parent.id'), primary_key=True)

    __mapper_args__ = {'polymorphic_identity': 'child'}

我希望能够使用Child的构造函数(例如Parent)创建Parent(type='child')的实例,但它不起作用。当我启动IPython时......

In [1]: from stackoverflow.question import Parent, Child

In [2]: from sqlalchemy import create_engine

In [3]: from sqlalchemy.orm import sessionmaker

In [4]: session = sessionmaker(bind=create_engine(...), autocommit=True)()

In [5]: with session.begin():
    p = Parent(type='child')
    session.add(p)
   ...:     
/.../lib/python3.4/site-packages/sqlalchemy/orm/persistence.py:155: SAWarning: Flushing object <Parent at 0x7fe498378e10> with incompatible polymorphic identity 'child'; the object may not refresh and/or load correctly
  mapper._validate_polymorphic_identity(mapper, state, dict_)

In [6]: session.query(Parent).all()
Out[6]: [<stackoverflow.question.Parent at 0x7fe498378e10>]

In [7]: session.query(Child).all()
Out[7]: []

这可能吗?这是个好主意吗?

2 个答案:

答案 0 :(得分:2)

绝对不是个好主意。而不是使用构造函数来做一些黑客攻击,你可以只有一个单独的帮助函数(工厂):

# create this manually
OBJ_TYPE_MAP = {
    # @note: using both None and 'parent', but should settle on one
    None: Parent, 'parent': Parent,
    'child': Child,
}
# ... or even automatically from the mappings:
OBJ_TYPE_MAP = {
    x.polymorphic_identity: x.class_
    for x in Parent.__mapper__.self_and_descendants
}
print(OBJ_TYPE_MAP)


def createNewObject(type_name, **kwargs):
    typ = OBJ_TYPE_MAP.get(type_name)
    assert typ, "Unknown type: {}".format(type_name)
    return typ(**kwargs)


a_parent = createNewObject(None, p_field1='parent_name1')
a_child = createNewObject(
    'child', p_field1='child_name1', c_field2='child_desc')

session.add_all([a_child, a_parent])

另一个注意事项:对于Parent我将为{'polymorphic_identity': 'parent'}定义一个值。它比None更清晰。

EDIT-1:使用构造函数

不是我推荐它,或者我真的知道我在这里做了什么,但如果你将以下定义的__new__添加到Parent类:

def __new__(cls, *args, **kwargs):
    typ = kwargs.get('type')  # or .pop(...)

    if typ and not kwargs.pop('_my_hack', None):
        # print("Special handling for {}...".format(typ))

        if typ == 'parent':
            # here we can *properly* call the next in line
            return super(Parent, cls).__new__(cls, *args, **kwargs)
        elif typ == 'child':
            # @note: need this to avoid endless recursion
            kwargs["_my_hack"] = True
            # here we need to cheat somewhat
            return Child.__new__(Child, *args, **kwargs)
        else:
            raise Exception("nono")
    else:
        x = super(Parent, cls).__new__(cls, *args, **kwargs)

    return x

您可以使用旧方法(当type=xxx没有传递给__init__时),或者通过提供参数来执行您要求的操作:

old_parent = Parent(field1=xxx, ...)
old_child = Child(field1=xxx, ...)
new_child = Parent(type='child', field1=xxx, ...)

同样,我不确定所有含义,特别是因为sqlalchemy还会覆盖创建例程并使用自己的元类。

答案 1 :(得分:1)

当使用sqlalchemy声明性映射时,会为每个类生成一个映射器。

你要做的是创建一个Parent的实例,它将作为Child的一个实例,这是你不能做的事情,至少不会诉诸黑客。

通过这个事实(你必须经历箍)这不是一个好主意。也许你根本不需要继承?

编辑

如果你不想有条件逻辑或查找,你必须根据用户输入选择一个类,你可以做这样的事情

cls = getattr(module_containing_the_classes, "<user_input>") 
cls(**kw)