sqlalchemy core将选择语句转换为更新语句

发布于 2025-02-08 06:03:12 字数 480 浏览 1 评论 0 原文

现在,我正在使用SQLalchemy Core(不是ORM)1.4,并且我有两个语句:

stmt1 = sa.select(groups).where(groups.c.id == group_id)
stmt2 = sa.update(groups).where(groups.c.id == group_id)

我想知道是否有一种方法可以将 stmt1 转换为 stmt2 stmt2 因为我想构建这样的声明:

# doing something to convert stmt1 to stmt2. is it possible?
stmt2 = stmt2.values(**my_dict)

谢谢您的帮助。

Now, I am using SQLAlchemy Core (NOT ORM) 1.4, and I've got two statements:

stmt1 = sa.select(groups).where(groups.c.id == group_id)
stmt2 = sa.update(groups).where(groups.c.id == group_id)

I would like to know if there's a way to convert stmt1 to stmt2 dynamically because I want to build a statement like this:

# doing something to convert stmt1 to stmt2. is it possible?
stmt2 = stmt2.values(**my_dict)

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混吃等死 2025-02-15 06:03:12

我自己找到答案,所以我在这里发布。

首先,我问是否可以将在更新使用sqlalchemy core的位置进行选择。

可以使用,但是它返回元组,因为我们通常选择或与一个或多个表一起选择或加入。

我的目的是获取一个必须更新的记录。

因此,我从更新语句中得出了Select语句:

stmt_update = sa.update(groups).where(groups.c.id == group_id)

stmt_select = sa.select(stmt_update.table).where(stmt_update.whereclause)

现在我不必传递非常相似的两个语句。谢谢。

I found an answer by myself, so I post here.

At first, I asked if I can convert select where to update where using SQLAlchemy core.

It's possible using Select().get_final_froms, but it returns a tuple because we usually select or join with one or more tables.

My purpose was fetching one record which has to be updated.

Thus, I derived the select statement from update statement like this:

stmt_update = sa.update(groups).where(groups.c.id == group_id)

stmt_select = sa.select(stmt_update.table).where(stmt_update.whereclause)

Now I don't have to pass very similar two statements. Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文