SQLAlchemy 核心 - 生成 PostgreSQL SUBSTRING 表达式? [英] SQLAlchemy Core - generating PostgreSQL SUBSTRING expression?

查看:92
本文介绍了SQLAlchemy 核心 - 生成 PostgreSQL SUBSTRING 表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PostgreSQL 中 SUBSTRING 的语法是 SUBSTRING( FROM FOR ).知道如何让 SQLAlchemy 核心生成它吗?我正在尝试 sqlalchemy.sql.expression.func,但这通常需要逗号分隔的符号.我没有看到解决这个问题的内置 Function.我不太确定 literaltext 是否可行.有什么想法吗?

The syntax for SUBSTRING in PostgreSQL is SUBSTRING(<text_expr> FROM <i> FOR <j>). Any idea how to make SQLAlchemy core generate that? I'm trying sqlalchemy.sql.expression.func, but that expects typically comma-separated notation. I don't see a built-in Function that addresses this. I'm not quite sure if literal or text would work. Any thoughts?

推荐答案

查看SqlAlchemy 测试,我发现sqlalchemy.sql.expression.func.substring 编译为SUBSTRINGPSQL:

Looking through the SqlAlchemy tests, I found that sqlalchemy.sql.expression.func.substring compiles to SUBSTRING for PSQL:

    def test_substring(self):
        self.assert_compile(
            func.substring("abc", 1, 2),
            "SUBSTRING(%(substring_1)s FROM %(substring_2)s "
            "FOR %(substring_3)s)",
        )
        self.assert_compile(
            func.substring("abc", 1),
            "SUBSTRING(%(substring_1)s FROM %(substring_2)s)",
        )

func.substring(str, from, [for]) 确实是你想要的.它是逗号分隔的",因为这就是 Python 方法的方式

func.substring(str, from, [for]) is indeed what you want. It is "comma-delineated" because that's how Python methods

如果您想自己生成 SQL,您可以执行类似 text("SUBSTRING('foo' FROM 1 FOR 2)") 之类的操作,但我不明白您为什么要这样做.

If you want to generate the SQL yourself, you could do something like text("SUBSTRING('foo' FROM 1 FOR 2)"), but I don't see why you would.

这篇关于SQLAlchemy 核心 - 生成 PostgreSQL SUBSTRING 表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆