首页 > 解决方案 > 加入表本身以查找具有相同关系的用户

问题描述

编辑:我的最后机会,更短的问题

我想用 sqlalchemy 做这个查询:

SELECT          first.* , 
                second.* , 
                third.* , 
                fourth.* , 
                mytable.* , 
                mytablesamerelationexist.* 
FROM            third 
LEFT OUTER JOIN mytable 
ON              third.id = mytable.id_third 
LEFT OUTER JOIN first 
ON              first.id = mytable.id_first 
LEFT OUTER JOIN second 
ON              second.id = mytable.id_second 
LEFT OUTER JOIN fourth 
ON              fourth.id = mytable.id_fourth 
LEFT OUTER JOIN mytable AS mytablesamerelationexist 
ON              mytable.id_second = mytablesamerelationexist.id_second 
AND             ( 
                                mytable.id_first = mytablesamerelationexist.id_first 
                OR              ( 
                                                mytable.id_first IS NULL 
                                AND             mytablesamerelationexist.id_first IS NULL ) 
                AND             mytable.id_third = {MYUSERPARAM} )

我想contains_eager用来访问对象,例如:

third.Mytable.MyTableSameRelationExist

查询的其余部分已经有效,我只需要进行自联接。

mytable.id_firstNULL

我的模型定义的最小值模型定义

长问题:

自己加入表的最佳方法是什么。我的表包含 4 个外键,我需要使用其中两个来查找相同的关系。这些外键之一可以为空。我需要得到关系 NULL == NULL。我不想在表中自己定义外键。

我尝试用类似的东西定义我的查询:

tablealias = aliased(MyTable)
...
outerjoin(tablealias , and_(or_(MyTable.id_first == tablealias.first,
                                and_(MyTable.id_first.is_(None),
                                     tablealias.id_first.is_(None))),
                                MyTable.id_second == tablealias.id_second,
                            tablealias.id_third == MYUSERID)).

我想以分层方式访问关系的结果,例如MyTable.self_child.

所以我在我的查询中添加:

options(contains_eager(MyTable.self_parent, MyTable.self_child))

我在MyTable类中尝试了很多东西来使关系“self_child”有效,而无需在自身上添加外键。

现在我已经将 id_second 定义为外键,因为 id_first 可以为空,我认为这可能是一个问题(我对吗?):

self_parent = relationship("MyTable",  primaryjoin='foreign(mytable.id_second) == remote(foreign(mytable.id_second))')
self_child = relationship("MyTable", primaryjoin='foreign(mytable.id_second) == remote(mytable.id_second)')

有了这个定义,当我初始化我的数据库时,我得到了错误:

“表”对象没有属性“id_second”

如果没有外键来自自身,我是否需要在我的类中添加关系?如果没有必要,我如何访问使用 contains_eager 定义的关系?如何正确定义?

编辑: 我通过了下面的错误,但我得到了很多不同的错误,比如

不明确的列名

或者

他们是同一个实体

对于最后一个错误,我在 Class 中定义了我的代码,例如:

self_child = relationship("MyTable", primaryjoin=and_(or_(foreign(id_first) == id_first, and_(foreign(id_first).is_(None), id_first.is_(None))).self_group(), foreign(id_second) == id_second).self_group(), remote_side=[id_second, id_first], lazy='joined')

查询:

tablealias = aliased(MyTable)
...
outerjoin(crotalias.self_child)
...
options(contains_eager(FirstLevel.mytable, crotalias.self_child))

如果我使用backref="self_parent"

我收到消息:

MyTable.self_child 和反向引用 MyTable.self_parent 都是相同的方向符号('ONETOMANY')。您的意思是在多对一上设置 remote_side 吗?

编辑:我的模型定义最少模型定义

任何提示将不胜感激。

标签: pythonsqlalchemypyramid

解决方案


我很惊讶,然后我没有收到这个问题的提示。

现在我使用了第二个想法,我在用户会话中添加了 id_first 和 id_second 的列表。因此,我可以遍历我的查询结果以匹配我的用户的值。


推荐阅读