【发布时间】:2017-05-21 22:18:01
【问题描述】:
我必须使用 SQLalchemy Core 表达式来获取对象,因为 ORM 不能执行“更新和返回”。 (ORM中的更新没有returning)
from sqlalchemy import update
class User(ORMBase):
...
# pure sql expression, the object returned is not ORM object.
# the object is a RowProxy.
object = update(User) \
.values({'name': 'Wayne'}) \
.where(User.id == subquery.as_scalar()) \
.returning() \
.fetchone()
当
db_session.add(object)
它报告UnmappedInstanceError: Class 'sqlalchemy.engine.result.RowProxy' is not mapped。
如何将 sql 表达式中的 RowProxy 对象放入 ORM 的标识映射中
?
【问题讨论】:
-
这似乎是
SQLAlchemy的疏忽,update-returning之类的东西(通常)返回正在更新的整个表,没有简单的方法来映射回对象。不幸的是,没有比这更令人满意的答案了。 -
您也许可以从
obj = User(**dict(object.items()))获得一些好处,但它似乎并非在所有情况下都有效。
标签: python orm sqlalchemy object-relational-model