连接表自己找到具有相同关系的用户

Mic*_*ael 5 python sqlalchemy pyramid

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

我想用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} )
Run Code Online (Sandbox Code Playgroud)

我想contains_eager用来访问像这样的对象:

third.Mytable.MyTableSameRelationExist
Run Code Online (Sandbox Code Playgroud)

查询的其余部分已经有效,我只需要进行自我加入.

mytable.id_firstNULL

我的模型定义最少 模型定义

长问题:

连接表本身的最佳方法是什么.我的表包含4个外键,我需要使用其中的两个才能找到相同的关系.其中一个外键可以为null.我需要得到关系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)).
Run Code Online (Sandbox Code Playgroud)

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

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

options(contains_eager(MyTable.self_parent, MyTable.self_child))
Run Code Online (Sandbox Code Playgroud)

我在MyTable类中尝试了很多东西,使关系'self_child'工作,而不需要在自身上添加外键.

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

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)')
Run Code Online (Sandbox Code Playgroud)

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

'table'对象没有属性'id_second'

如果没有外键,我是否需要在我的班级中添加关系?如果没有必要,我如何访问contains_eager定义的关系?如何正确定义?

编辑: 我传递下面的错误,但我得到了很多不同的错误,如

模糊的列名

要么

他们是同一个实体

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

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')
Run Code Online (Sandbox Code Playgroud)

查询:

tablealias = aliased(MyTable)
...
outerjoin(crotalias.self_child)
...
options(contains_eager(FirstLevel.mytable, crotalias.self_child))
Run Code Online (Sandbox Code Playgroud)

如果我使用 backref="self_parent"

我得到了消息:

MyTable.self_child和back-reference MyTable.self_parent都是相同的方向符号('ONETOMANY').你的意思是在多对一方设置remote_side吗?

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

任何提示将不胜感激.

Mic*_*ael 0

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

现在我使用了第二个想法,我在用户会话中添加 id_first 和 id_second 列表。因此,我能够循环查询的结果以与用户的值相匹配。