Skip to content

sqlalchemy.sql.expression.table & schema #5309

Closed
@DylanModesitt

Description

@DylanModesitt

table(...) in the expression language does not accept a schema (presumably, intentionally). When wanting to use it for schemas other than the default on some dialects (Oracle, for example), the resulting quoted identifier will not work.

sqlalchemy.select(["*"]).select_from(sqlalchemy.table("someschema.SOMETABLE"))

produces (with the oracle dialect)

SELECT * 
FROM "someschema.SOMETABLE" 

which is invalid. However, it happens that because table is produced from TableClause, setting .schema on the resulting table allows a correct query.

the_table = sqlalchemy.table("SOMETABLE")
the_table.schema = "someschema"
sqlalchemy.select(["*"]).select_from(the_table)

gives

SELECT * 
FROM "someschema"."SOMETABLE" 

Possible Solution

Is it possible for TableClause to take a keyword-only schema parameter? Or would this cause breaking changes when used with sqlalchemy.Table when a table is bound to a schema by metadata?

If that is not possible, is it acceptable for table do slightly more work than the public_factory and accept this keyword-only argument itself - since that appears to be the intended entry-point for TableClause when not using .Table.

Or are either of these changes too fragile? I would be happy to work on a PR for this if any solution is acceptable. I think this is a fairly niche feature that is already implicitly supported by the expression-language "compiler" with .schema - but it would be nice to have on construction.

Alternatively

One can do as I do now by setting .schema after creating the TableClause with table.

Activity

added
PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteers
use casenot really a feature or a bug; can be support for new DB features or user use cases not anticipated
and removed
requires triageNew issue that requires categorization
on May 5, 2020
added this to the 1.3.x milestone on May 5, 2020
zzzeek

zzzeek commented on May 5, 2020

@zzzeek
Member

Is it possible for TableClause to take a keyword-only schema parameter? Or would this cause breaking changes when used with sqlalchemy.Table when a table is bound to a schema by metadata?

this should be possible, there might be a lot of cases to get working but then again maybe not. it's just one extra attribute, and because it's being added, there's no issue of backwards incompatibility. I don't see why it would interfere with the Table subclass, which would continue to accommodate "schema" and "name" in its own way.

If that is not possible, is it acceptable for table do slightly more work than the public_factory and accept this keyword-only argument itself - since that appears to be the intended entry-point for TableClause when not using .Table.

looking at TableClause that is likely the reason it doesnt have any options, because we are with Python 2 compatibility for the next year at least, we would have to add **kw to the class, so likely there should be "schema = kw.pop('schema', None)" and then "if kw: raise ArgumentError".

Or are either of these changes too fragile? I would be happy to work on a PR for this if any solution is acceptable. I think this is a fairly niche feature that is already implicitly supported by the expression-language "compiler" with .schema - but it would be nice to have on construction.

sure !

Alternatively

One can do as I do now by setting .schema after creating the TableClause with table.

a very effective workaround that is probably happening in the test suite too.

added 3 commits that reference this issue on May 5, 2020
1684e3a
ce982fc
59b73db
sqla-tester

sqla-tester commented on May 6, 2020

@sqla-tester
Collaborator

Dylan Modesitt has proposed a fix for this issue in the master branch:

Add 'schema' parameter to table https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1944

sqla-tester

sqla-tester commented on May 10, 2020

@sqla-tester
Collaborator

Dylan Modesitt has proposed a fix for this issue in the master branch:

Add 'schema' parameter to table https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1944

sqla-tester

sqla-tester commented on May 15, 2020

@sqla-tester
Collaborator

Dylan Modesitt has proposed a fix for this issue in the rel_1_3 branch:

Add 'schema' parameter to table https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/1976

2 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PRs (with tests!) welcomea fix or feature which is appropriate to be implemented by volunteerssqluse casenot really a feature or a bug; can be support for new DB features or user use cases not anticipated

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zzzeek@sqla-tester@DylanModesitt

        Issue actions

          sqlalchemy.sql.expression.table & schema · Issue #5309 · sqlalchemy/sqlalchemy