带空格的 SQLAlchemy 列名 [英] SQLAlchemy column name with space

查看:41
本文介绍了带空格的 SQLAlchemy 列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据包含空格的列过滤表格.

I'm trying to filter a table on a column that contain spaces.

...
events = database_session.query(table)
events.filter(table.column with space == 'xvalue')   < -- I want to do that
...

肯定有一种简单的方法可以做到这一点,但我似乎在任何地方都找不到.

There is for sure a simple way of doing that, but I can't seem to find it anywhere.

推荐答案

有两种方法可以解决这个问题.

There are two ways to resolve this.

  1. 在定义表时,您需要使用 key 参数指定别名

t_table_name = Table(
    'tablename',
    metadata,
    Column('SQL Column', Integer, key='sql_column')
)

  1. 将 ORM 类定义为

class Employee(Base):
    emp_name = Column("employee name", String)

这篇关于带空格的 SQLAlchemy 列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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