python - SQLAlchemy 引用表名 - 无法重新定义 'quote' 或 'quote_schema' 参数

标签 python python-2.7 sqlalchemy

从 sqlalchemy 0.8 升级到 1.0.4 时,我的 ORM 因错误无法重新定义“quote”或“quote_schema”参数而中断

我连接到一个 sybase 数据库,并使用一个 declarative_base

Base = declarative_base()

使用标准方法创建下面的映射

class RiskAggregationGroup(Base):
    __tablename__ = 'RISK_AGGREGATION_GROUP'
    __table_args__ = {'quote':False,'extend_existing':True}

    id = Column(Integer, name='id_risk_agg', primary_key=True)
    name = Column(String(50), name='nm_risk_agg')
    description = Column(String(100), name='tx_desc')

这在 sqlalchemy 0.8 中运行良好,但在 1.0.4 中中断,因为它不喜欢我将 quote 指定为表 arg。我已经尝试了很多方法来解决这个问题,将它设置在底部,例如

class Base(object):

    __table_args__ = {'quote':False,'extend_existing':True}

Base = declarative_base(cls=Base)

抛出同样的错误。如果我将其更改为使用 @declared_attr,则不会关闭引用。我无法更改 sybase 设置并且我的表名全部大写(这是引用的原因)。我在这里定义了大约 20 个表,所以我不愿意将它们全部更改为表创建,例如:

class RiskAggregationGroup(Base):
    __tablename__ = 'RISK_AGGREGATION_GROUP'

    __table__ = Table(__tablename__, Base.metadata, 
    Column(Integer, name='id_risk_agg', primary_key=True, key='id'),
    Column(String(50), name='nm_risk_agg', key='name'), quote=False)

有没有人有更优雅的解决方案,到目前为止谷歌没有让我失望?

最佳答案

从 sqlalchemy google group 得到了答案

https://groups.google.com/forum/#!topic/sqlalchemy/xIPnU89GKFI

非常感谢 Michael Bayer。解决方案是不设置 quote:False,而是将引号字符设置为 []。

e = create_engine("sybase://")

# if not using quote=False, this will change the quoting character
e.dialect.identifier_preparer.initial_quote = '['
e.dialect.identifier_preparer.final_quote = ']'

关于python - SQLAlchemy 引用表名 - 无法重新定义 'quote' 或 'quote_schema' 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30474216/

相关文章:

python - pandas 中单列的多个值集的差异

python - 在 api-view 中获取序列化器字段值

python - 如何在被调用的方法中获取调用者的方法名?

python - 在 Django 3.1 中管理静态文件

python - 当第二个列表包含 numpy 数组时,根据 python 中的其他列表对列表进行排序不起作用

python - 使用 sqlalchemy 多态关系删除整个层次结构

python - 如何将输入读取为数字?

python - 在 Python 中 n 次失败后退出无限循环的最佳方法是什么?

python - 如何通过代码使用 Python Alembic 运行迁移?

python - 将 SQLAlchemy 模型保存到文件