gpt4 book ai didi

python - 使用父构造函数在 SQLAlchemy session 中添加子类

转载 作者:行者123 更新时间:2023-11-28 16:32:27 26 4
gpt4 key购买 nike

我有一个在 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 的构造函数创建 Child 的实例(例如 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]: []

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

最佳答案

绝对不是一个好主意。您可以只使用一个单独的辅助函数(工厂),而不是使用构造函数来做一些 hack:

# 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 还覆盖了创建例程并使用了它自己的元类。

关于python - 使用父构造函数在 SQLAlchemy session 中添加子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30518484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com