【问题标题】:SQLAlchemy map columns with spaces/ brackets in them to class attributes [duplicate]SQLAlchemy将带有空格/括号的列映射到类属性[重复]
【发布时间】:2017-07-13 00:30:36
【问题描述】:

如何使用类属性映射表中的列。我正在使用的一些列名是 table.[An example] 和 table。

【问题讨论】:

  • 您可以为属性指定different name,而不是实际的列名。

标签: python sqlalchemy mapping


【解决方案1】:

@杰森卡 有两种方法可以解决这个问题。你可以参考https://stackoverflow.com/a/48686249/1055704

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

    table_name = Table( 'table)name', metadata, Column('SQL Column',Integer, key='sql_column'))
    
  2. 创建实体类时可以指定字段名

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2013-08-01
    • 2015-01-08
    • 2023-02-11
    • 2019-05-24
    • 2012-10-13
    • 1970-01-01
    相关资源
    最近更新 更多