我有一个名为的父表pbx_point
,它有一point_type
列。我还有一个名为的子表pbx_route
,其中有一列名为point_id
指向pbx_point
。
我想使用 sqlalchemy 的连接表继承通过声明性基础来关联这两个表,并使用多态继承
这很好用 - 或者更确切地说,如果没有以下附加约束,它会:pbx_point
还有一个称为initial_route_id
指向的外键pbx_route
。
我也在下面使用反射,但数据库就像我上面描述的那样。我得到的错误是sqlalchemy.exc.AmbiguousForeignKeysError: Can't determine join between 'pbx_point' and 'pbx_route'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly.
.
这是有道理的,因为“幕后”描述性基础是在两个映射类上创建一个 relationship() 属性。我希望它选择pbx_route.point_id
作为 parent_id 链接,但它也可以看到该pbx_point.initial_route_id
列。如果我正在创建这个关系(),这将很容易解决,但我不是 - 声明式继承系统是。
是否有其他参数可以传递给__mapper_args__
,例如polymorphic_parent_col
可以让我指定我想要的外键?如果没有,我该如何解决这个问题?
谢谢。
class MyBase(DeferredReflection):
@declared_attr
def __tablename__(cls):
return cls.__name__.lower()
Base = declarative_base(cls=MyBase)
class pbx_point(Base):
__mapper_args__ = dict(
polymorphic_on='point_type',
with_polymorphic='*',
)
class pbx_route(pbx_point):
__mapper_args__ = dict(polymorphic_identity='pbx.route')
这是我得到的堆栈跟踪:
Traceback (most recent call last):
File "db.py", line 50, in <module>
Base.prepare(engine)
File "env/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 431, in prepare
thingy.map()
File "env/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 379, in map
**mapper_args
File "env/local/lib/python2.7/site-packages/sqlalchemy/orm/__init__.py", line 1147, in mapper
return Mapper(class_, local_table, *args, **params)
File "env/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 213, in __init__
self._configure_inheritance()
File "env/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 517, in _configure_inheritance
self.local_table)
File "env/local/lib/python2.7/site-packages/sqlalchemy/sql/util.py", line 397, in join_condition
"join explicitly." % (a.description, b.description))
sqlalchemy.exc.AmbiguousForeignKeysError: Can't determine join between 'pbx_point' and 'pbx_route'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly.
这表明它在 mapper.py * 的第 1032 行死亡。上面几行引用了 mapper kwarg inherit_condition
,这似乎是我需要的。
[*]:源链接调整为 1.3.11 版本(以前的 URL 现在是 404s)