python - 加入表本身以查找具有相同关系的用户

标签 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} )

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

third.Mytable.MyTableSameRelationExist

查询的其余部分已经工作,我只需要进行自连接。

mytable.id_first 可以是NULL

有我的模型定义的最小值model definition

长问题:

在自身上连接表的最佳方式是什么。我的表包含 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 可以为 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)')

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

'Table' object has no attribute 'id_second'

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

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

ambiguous column name

they are the same entity

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

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 and back-reference MyTable.self_parent are both of the same direction symbol('ONETOMANY'). Did you mean to set remote_side on the many-to-one side ?

编辑: 有我的模型定义的最小值 model definition

任何提示将不胜感激。

最佳答案

令我惊讶的是我没有收到关于这个问题的提示。

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

关于python - 加入表本身以查找具有相同关系的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54182742/

相关文章:

mysql - 如何将此原始 SQL 转换为 sqlalchemy 查询?

python - 如何在 MVC 设计的应用程序中使用 SQLAlchemy?

python - Pyramid_beaker : session. type = cookie 不安全?

python - 使用 uWSGI 和 Cherokee 部署 Pyramid 应用程序

python - 使用 SQLAlchemy 在 Pyramid 中创建包含少量相关表的查询

python - .communicate() 和 .communicate()[0] 有什么区别?

Python 输入验证大于零的整数

python - 如何设置热图纵横比

Python如何从for循环中的字典列表中删除空行?

python - SQLAlchemy 类型错误 : where() got an unexpected keyword argument