2
votes

I have been struggling on this for a while now and did not find an answer yet, or maybe I already have seen the answer and just didnt get it - however, I hope I am able to describe my problem.

I have a MS SQL database in which the tables are grouped in namespaces (or whatever it is called), denoted by Prefix.Tablename (with a dot). So a native sql statement to request some content looks like this:

    SELECT TOP 100 
     [Value], [ValueDate]
    FROM [FinancialDataBase].[Reporting].[IndexedElements]

How to map this to sqlalchemy? If the "Reporting" prefix would not be there, the solution (or one way to do it) looks like this:

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.orm import sessionmaker

def get_session():
    from urllib.parse import quote_plus as urllib_quote_plus

    server = "FinancialDataBase.sql.local"
    connstr = "DRIVER={SQL Server};SERVER=%s;DATABASE=FinancialDataBase" % server
    params = urllib_quote_plus(connstr)
    base_url = "mssql+pyodbc:///?odbc_connect=%s" % params

    engine  = create_engine(base_url,echo=True)
    Session = sessionmaker(bind=engine)
    session = Session()

    return engine, session

Base = declarative_base()

class IndexedElements(Base):
    __tablename__ = "IndexedElements"

    UniqueID = Column(String,primary_key=True)
    ValueDate = Column(DateTime)
    Value = Column(Float)

And then requests can be done and wrapped in a Pandas dataframe for example like this:

import pandas as pd

engine, session = get_session()

query = session.query(IndexedElements.Value,IndexedElements.ValueDate)

data = pd.read_sql(query.statement,query.session.bind)

But the SQL statement that is compiled and actually executed in this, includes this wrong FROM part:

FROM [FinancialDataBase].[IndexedElements]

Due to the namespace-prefix it would have to be

FROM [FinancialDataBase].[Reporting].[IndexedElements]

Simply expanding the table name to

__tablename__ = "Reporting.IndexedElements"

doesnt fix it, because it changes the compiled sql statement to

FROM [FinancialDataBase].[Reporting.IndexedElements]

which doesnt work properly.

So how can this be solved?

1
The term you're looking for is schema. SQLAlchemy supports schemas both in the metadata as the default schema to use and per table. With Declarative you should pass a per table schema in the __table_args__. - Ilja Everilä
Thanks Ilja, "schema" nailed it! I will create an answer referring to your comment! - joaquinn

1 Answers

4
votes

The answer is given in the comment by Ilja above:

The "namespace" is a so called schema and has to be declarated in the mapped object. Given the example from the opening post, the mapped table has to be defined like this:

class IndexedElements(Base):
    __tablename__ = "IndexedElements"
    __table_args__ = {"schema": "Reporting"}

    UniqueID = Column(String,primary_key=True)
    ValueDate = Column(DateTime)
    Value = Column(Float)

Or define a base class containing these informations for different schemata. Check also "Augmenting the base" in sqlalchemy docs: http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/mixins.html#augmenting-the-base