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

时间:2015-05-27 05:55:55

标签: python python-2.7 sqlalchemy

虽然从sqlalchemy 0.8升级到1.0.4,但我的ORM因错误而无法重新定义'引用'或者' quote_schema'参数

我连接到sybase db,并使用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中有所突破,因为它并不像我指定引用作为表格arg。我已经尝试了很多东西来解决这个问题,将其设置在基础上,例如

class Base(object):

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

Base = declarative_base(cls=Base)

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

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)

有没有人有更优雅的解决方案,到目前为止谷歌已经失败了我?

1 个答案:

答案 0 :(得分:4)

从sqlalchemy google小组得到了答案

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 = ']'