2017-01-18 89 views
0

我是SQLAlchemy的新手,所以请原谅一定是一个基本问题。基于另一个表中的值的简单SQLAlchemy更新表

我有一个数据库表properties(映射在SQLALchemy中作为对象Property),它包含一个字段MEBID。我有另一个表mebs(在SQLAlchemy中映射为MEB)。我想将properties.MEBID字段设置为mebs.id,其中properties.PostCode == mebs.PostCode。

我可以简单地在SQL中使用命令

update properties, mebs set properties.mebid = mebs.id where mebs.PostCode = properties.PostCode 

做到这一点,但我在SQLAlchemy中做挣扎。如果我尝试的命令

session.query(Property, MEB).\ 
    filter(Property.PostCode == MEB.PostCode).\ 
    update({Property.MEBID : MEB.id}) 

我得到

InvalidRequestError: This operation requires only one Table or entity be specified as the target. 

我知道这一定是基本的,因为它是这样一个基本的操作,但它是如何做不能工作了。

回答

1

更新:

for prop, meb in session.query(Property, MEB).filter(Property.PostCode == MEB.PostCode).all(): 
    prop.MEBID=meb.id 
    session.add(prop) 
相关问题