Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy quoting of table names - Can't redefine 'quote' or 'quote_schema' arguments

Whilst upgrading from sqlalchemy 0.8 to 1.0.4 my ORM has broken with the error Can't redefine 'quote' or 'quote_schema' arguments

I connect to a sybase db, and use a declarative_base

Base = declarative_base()

Using a standard method to create the mapping below

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')

This worked fine in sqlalchemy 0.8 but breaks in 1.0.4 as it doesn't like me specifying quote as a table arg. I've tried a whole host of things to get around this, setting it in the base, e.g.

class Base(object):

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

Base = declarative_base(cls=Base)

throws the same error. If I change it to use the @declared_attr the quoting is not turned off. I'm unable to change the sybase settings and my table names are all caps (which is the cause of the quoting). I've got about 20 tables defined here, so am loathe to change them all to Table creations, such as:

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)

Does anyone have a more elegant solution, so far google has failed me?

like image 300
Adz Avatar asked May 27 '15 05:05

Adz


People also ask

What does DB Create_all () do?

create_all() function to create the tables that are associated with your models. In this case you only have one model, which means that the function call will only create one table in your database: from app import db, Student. db.

What is the use of MetaData in SQLAlchemy?

It is used when reflecting and creating databases in Python (using SQLAlchemy package). MetaData is a container object that keeps together many different features of a database (or multiple databases) being described.

What is nullable SQLAlchemy?

From SQLAlchemy docs: nullable – If set to the default of True, indicates the column will be rendered as allowing NULL, else it's rendered as NOT NULL. This parameter is only used when issuing CREATE TABLE statements.


1 Answers

Got an answer to this from the sqlalchemy google group

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

Huge thanks Michael Bayer. Solution is to Not set quote:False, but to set the quote characters to [].

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 = ']'
like image 89
Adz Avatar answered Sep 30 '22 18:09

Adz