我有一个类继承方案,布局在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'}
我想能够使用Parent
(如Parent(type='child')
)的构造函数创建Child
的实例,但它不起作用。当我启动ippython时…
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]: []
这可能吗?这是个好主意吗?
这绝对不是一个好主意。不需要使用构造函数来做一些修改,您可以只使用一个单独的辅助函数(工厂):
# 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: using Constructor
不是我推荐它,或者我真的知道我在这里做什么,但是如果您将__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还覆盖了创建例程并使用了自己的元类。
问题是,当使用sqlalchemy声明性映射时,会为每个类生成一个映射器。
你要做的是让Parent的一个实例表现得像Child的一个实例,这是你做不到的,至少在不诉诸黑客的情况下。
从这个事实来看(你必须经过重重考验)这不是个好主意。也许你根本不需要继承?
编辑如果你不想有条件逻辑或查找,你必须根据用户输入选择一个类,你可以这样做
cls = getattr(module_containing_the_classes, "<user_input>")
cls(**kw)