Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 标签在sqlalchemy、数据库、ORM中的使用_Python_Database_Python 3.x_Postgresql_Sqlalchemy - Fatal编程技术网

Python 标签在sqlalchemy、数据库、ORM中的使用

Python 标签在sqlalchemy、数据库、ORM中的使用,python,database,python-3.x,postgresql,sqlalchemy,Python,Database,Python 3.x,Postgresql,Sqlalchemy,我有这段代码,它工作得很好,但我只想在这里添加一件事,那就是标签的使用。 例如,如何在其他标签中使用名称标签“id1” 我希望我的代码是这样的: q = session.query( label('id1', func.least(Communication.initiator_id, Communication.receiver_id)), label('id2', func.greatest(Communication.initiator_id, Communication.

我有这段代码,它工作得很好,但我只想在这里添加一件事,那就是标签的使用。 例如,如何在其他标签中使用名称标签“id1”

我希望我的代码是这样的:

q = session.query(
    label('id1', func.least(Communication.initiator_id, Communication.receiver_id)),
    label('id2', func.greatest(Communication.initiator_id, Communication.receiver_id)),
    label('orga_id1', func.greatest(0, 1)),
    label('orga_id2', func.greatest(0, 1)),
    label('nb', func.count(Communication.id))
).filter(
    or_(
        Communication.initiator_id == people_id,
        Communication.receiver_id == people_id
    )
).order_by("nb").group_by('id1', 'id2').all()

有人能告诉我如何在查询中使用标签的值吗

不能在子查询中引用父查询的输出列,但可以引用父查询的源表:

q = session.query(
    label('id1', func.least(Communication.initiator_id, Communication.receiver_id)),
    label('id2', func.greatest(Communication.initiator_id, Communication.receiver_id)),
    label('orga_id1', func.greatest(session.query(People).filter_by(id = "And here somehow i want the value that was created on first label with name 'id1'").first().orga_id, -1)),
    label('orga_id2', func.greatest(session.query(People).filter_by(id = "And here somehow i want the value that was created on second label with name 'id2'").first().orga_id, -1)),
    label('nb', func.count(Communication.id))
).filter(
    or_(
        Communication.initiator_id == people_id,
        Communication.receiver_id == people_id
    )
).order_by("nb").group_by('id1', 'id2').all()
不起作用,但是

select 1 as one, (select one) as two;
是的。另一方面,您可以先执行查询以获取通信和计数,然后在顶部添加人员的组织ID。按照您最初设想的方式使用标量子查询:

select 1 as one, (select s.i) as two from generate_series(2, 2) s(i);
使用左连接:

id1 = func.least(Communication.initiator_id, Communication.receiver_id)
id2 = func.greatest(Communication.initiator_id, Communication.receiver_id)

sq = session.query(id1.label('id1'),
                   id2.label('id2'),
                   func.count(Communication.id).label('nb')).\
    filter(or_(Communication.initiator_id == people_id,
               Communication.receiver_id == people_id)).\
    order_by('nb').\
    group_by('id1', 'id2').\
    subquery()

orga_id1 = session.query(People.orga_id).filter_by(id=sq.c.id1).as_scalar()
orga_id2 = session.query(People.orga_id).filter_by(id=sq.c.id2).as_scalar()

q = session.query(sq.c.id1,
                  sq.c.id2,
                  func.greatest(orga_id1, -1).label('orga_id1'),
                  func.greatest(orga_id2, -1).label('orga_id2'),
                  sq.c.nb).\
    all()

您不能以尝试的方式在子查询中引用父查询的选择列表中的标签。另一方面,您可以引用父表,因此可以使用
Communication.initiator\u id
等,这要感谢SQLAlchemy的。但是:
Query.first()。您想使用,请选择
People.id
。但例如,在使用第一个示例结束查询后,我是否可以更改
q[0].orga_id1=“new value”
?如果是,我该怎么做?bcs如果我确实喜欢
q[0]。orga_id1=11
我得到错误
无法设置属性
我不确定我是否遵循。另一件事:如果您按id筛选人员,请先选择,然后使用其id,为什么您需要子查询?我的意思是,
session.query(People).filter_by(id=the_id1).first().id
将等于id1。是否有输入错误?编辑,我正在搜索人员所在的组织,bcs我需要人员的id以及他们正在工作的组织的id,但仅限于id1 id2列中的id,以及具有组织id的2列(非常感谢,这对我有帮助!)
from sqlalchemy.orm import aliased

id1 = func.least(Communication.initiator_id, Communication.receiver_id)
id2 = func.greatest(Communication.initiator_id, Communication.receiver_id)

people1 = aliased(People)
people2 = aliased(People)

sq = session.query(id1.label('id1'),
                   id2.label('id2'),
                   func.count(Communication.id).label('nb')).\
    filter(or_(Communication.initiator_id == people_id,
               Communication.receiver_id == people_id)).\
    order_by('nb').\
    group_by('id1', 'id2').\
    subquery()

q = session.query(sq.c.id1,
                  sq.c.id2,
                  func.coalesce(people1.orga_id, -1).label('orga_id1'),
                  func.coalesce(people2.orga_id, -1).label('orga_id2'),
                  sq.c.nb).\
    outerjoin(people1, people1.id == sq.c.id1).\
    outerjoin(people2, people2.id == sq.c.id2).\
    all()