从 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/