Search code examples
pythonsqlsqlalchemysybaseintrospection

How to perform table reflection / introspection in SQLAlchemy from a Sybase db with Python?


Some helpful stackoverflow users pointed out that neither the pyodbc nor python-sybase plugins support Sybase table reflection within SQLAlchemy.

So, my question is: are there any alternatives that exist for reflecting database metadata for tables in a Sybase deployment? Or if that's not possible, is there a clever way to monkey-patch and/or hack together a solution to pull down table metadata for parsing and query-building?


Solution

  • it all comes down to calling upon the information schema provided by the database, which is a set of tables and/or views which you can select from, that then return data about tables, columns, and everything else.

    In the case of Sybase, they have a really, really tough format for their information schema (mostly due to the 16 + 16 column format of representing foreign key constraints) , so when I created the Sybase dialect, I left the reflection part of the job as a "todo". There's no technical reason it can't be done, it's just a long and tedious job and we have virtually nobody using the Sybase dialect as of yet.

    If you want to work out some of the basic queries to get at table and column names, we can commit those straight into SQLAlchemy. The schema you'd be working with is documented in this section: http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.tables/html/tables/tables2.htm and in there you'd be looking at sysobjects, syscolumn, and sysreferences.