MSSQL dialect should use the schema, owner, and name of a table to compose the table's name

Issue #594 resolved
Former user created an issue

It is possible for a MSSQL connection attached to one database to reference objects in another database by using the objects' full names in the form, <database_name>.<owner_name>.<table_name>. The schema.Table constructor takes a schema argument and an owner argument already. These arguments should be used so that the schema argument takes the place of the database name in the format described above.

So the following table definition:

t = Table('a_table', meta, Column('col1', Integer), Column('col2', Integer), schema='database_name', owner='owner_name')

and select construction:

select(t)

would yield the following SQL when compiled:

SELECT a_table_xxxx.col1, a_table_xxxx.col2 FROM database_name.owner_name.a_table AS a_table_xxxx

Please find included a simple test case that demonstrates the desired behavior.

Comments (7)

  1. paj
    • assigned issue to
    • changed milestone to 0.4.0
    • marked as minor

    I agree we should support this. I did wonder if as a hack you could just set schema to be "database.schema", but in fact this doesn't work due to identifier quoting.

    I think using the "owner" argument is abusing the meaning of that, to the point of being confusing. I wonder if we should add another argument to table, perhaps "database".

    I'll have a go at coding this up, but I am going to set the priority to low.

  2. Mike Bayer repo owner

    "owner" is deprecated on Table. For this, I would want to either have "schema" be sent in as two-token'ed name, i.e. "ownername.schemaname", and have quoting logic that needs them separate to use "split()" to find them when needed. Alternatively we could support "schema" being represented optionally as a list/tuple of names. In any case its fine to have the IdentifierPreparer, or MS-SQL's subclass of it, check for this condition. Around compiler.py there's a lot of calls to preparer.quote(table, table.schema) - its fine to change these into preparer.format_table() calls, or alternatively add a flag to preparer.quote() called "split=True" if we know we're sending in a schema name that might be dot-separated.

  3. Log in to comment