英文:
SQLAlchemy: pass extra options into session.merge
问题
The code you provided is trying to pass the option 'with_for_update' to the session.merge
method, but it's causing an exception. To add the 'with_for_update' option correctly, you should use the with_for_update
method within a Session
context, like this:
place_model = type(place)
try:
async with session.begin_nested():
place_merged = await session.merge(place).with_for_update()
except sa_exc.IntegrityError as err:
# Handle the exception here
# ...
By chaining the with_for_update()
method to the result of session.merge(place)
, you should be able to apply the 'with_for_update' option correctly.
英文:
In documentation on .merge is written :https://docs.sqlalchemy.org/en/20/orm/session_api.html#sqlalchemy.orm.Session.merge that it is possible to pass extra options which lately will be applied into session.get. Is session.get documentation is written that is is possible to use with_for_update option https://docs.sqlalchemy.org/en/20/orm/session_api.html#sqlalchemy.orm.Session.get.params.with_for_update
place_model = type(place)
try:
async with session.begin_nested():
place_merged = await session.merge(place, options={'with_for_update': True})
except sa_exc.IntegrityError as err:
....
....
I have been trying to use it like this but got an exception - sqlalchemy.exc.ArgumentError: ExecutionOption Core or ORM object expected, got 'with_for_update'.
Question is - how to add option with_for_update into session.merge correctly?
答案1
得分: 1
以下是您要翻译的内容:
这是我认为的一个误解。
merge docs 中写道:
options –
可选的加载器选项序列,将应用于从数据库加载现有对象的合并操作的 Session.get() 方法。
我认为这对应于 Session.get
的 options 参数:
options – 可选的加载器选项序列,将应用于查询(如果发出查询的话)。
with_for_update 是一个独立的参数,不能通过 Session.merge
传递。
英文:
This is a misunderstadning I think.
The merge docs state:
> options –
> optional sequence of loader options which will be applied to the Session.get() method when the merge operation loads the existing version of the object from the database.
I believe this maps to the options parameter of Session.get
:
> options – optional sequence of loader options which will be applied to the query, if one is emitted.
with_for_update is a separate parameter, it cannot be passed via Session.merge
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论