Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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/7/sqlite/3.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:链接表的批量相关更新_Python_Sqlite_Orm_Sqlalchemy_Correlated - Fatal编程技术网

Python Sqlalchemy:链接表的批量相关更新

Python Sqlalchemy:链接表的批量相关更新,python,sqlite,orm,sqlalchemy,correlated,Python,Sqlite,Orm,Sqlalchemy,Correlated,我已经用SQLAlchemy ORM填充了两个相关的表。但是,单个记录尚未链接,即外键列保留为空。我需要根据不同列的匹配情况批量更新外键列 举例说明: class Location(Base): __tablename__ = 'locations' id = Column(Integer, primary_key=True) x = Column(Float) y = Column(Float) class Stopover(Base): __tab

我已经用SQLAlchemy ORM填充了两个相关的表。但是,单个记录尚未链接,即外键列保留为空。我需要根据不同列的匹配情况批量更新外键列

举例说明:

class Location(Base):
    __tablename__ = 'locations'
    id = Column(Integer, primary_key=True)
    x = Column(Float)
    y = Column(Float)


class Stopover(Base):
    __tablename__ = 'stopovers'
    id = Column(Integer, primary_key=True)
    x = Column(Float)
    y = Column(Float)
    location_id = Column(Integer, ForeignKey("locations.id"))
    location = relationship("Location", backref=backref("stopovers"))
因此,本质上,我需要通过匹配“x”和“y”列,将20000多条“中途停留”记录中的每一条与“位置”关联起来,即批量更新“位置id”列

此代码正确生成_location_id uu:

for stpvr in session.query(Stopovers).all():
       stpvr.location_id = session.query(Location.id).\
                           filter_by(x=stpvr.x).\
                           filter_by(y=stpvr.y).one()[0]
       session.commit()
但是,它似乎不起作用-通过Sqliteman查看数据库表明位置ID尚未更新。此外,我猜一定有一种更优雅的方式来处理这个问题

在文档中,我发现最接近我要找的东西。然而,文档只引用SQL表达式语言,而我使用的是ORM。我是SQLAlchemy的新手,我尝试将文档翻译成ORM的尝试没有成功


如果您能帮助我找到执行此批量更新的最优雅方式,我将不胜感激。提前感谢。

SQLAlchemy可以分层工作。在底层,SQLAlchemy提供了诸如使用各种数据库驱动程序的数据库统一接口和连接池实现等功能。上面有一个SQL表达式语言,允许您使用Python对象定义数据库的表和列,然后使用这些对象使用SQLAlchemy提供的API创建SQL表达式。然后是ORM。ORM构建在这些现有层上,因此即使您使用ORM,您仍然可以下拉使用表达式API。使用构建在ORM上的声明性模型,您的级别甚至更高

大多数表达式API基于SQLAlchemy表对象和列。这些表可以通过映射类上的_table _;属性访问,列可以作为映射类上的属性使用。因此,即使您处于声明性级别,您仍然可以在使用使用声明性映射的模型时利用您在那里可以使用的大部分内容。所以,这个

…可以通过使用_table__)属性和声明性列转换为声明性ORM模型

>>> stmt = select([Addresses.email_address]).\
...             where(Addresses.user_id == Users.id).\
...             limit(1)
>>> conn.execute(Users.__table__.update().values(fullname=stmt)) 
下面是我相信您的相关查询的样子

stmt = select([Location.id]).\
    where(and_(Location.x==Stopover.x, Location.y==Stopover.y)).limit(1)

conn.execute(Stopover.__table__.update().values(location_id=stmt)
生成的SQL:

更新中途停留设置位置\u id=选择位置.id 从地点 其中locations.x=中途站.x和locations.y=中途站.y 限度抵消
谢谢你的解答和解释!“选择…”。。。“where”方法很好用。然而,“查询…”。。。filter方法抛出AttributeError:“Query”对象没有属性“self\u group”。我很好奇为什么。再次感谢您的帮助!事实上,我不知道这是怎么回事。我一度认为它可以工作,但我想我没有收到相同的错误信息。在任何情况下,使用表达式API都是有效的,因此您可以坚持使用它。
stmt = select([Location.id]).\
    where(and_(Location.x==Stopover.x, Location.y==Stopover.y)).limit(1)

conn.execute(Stopover.__table__.update().values(location_id=stmt)