Skip to content

query.get() fails on tables with a schema #456

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot
Collaborator

Migrated issue, originally created by paj (@paj)

MSSQL supports schemas (as in schema.table) and SA supports this. However, a simple query.get() fails with a KeyError inside MSSQLCompiler.visit_column. A minimal test case is attached.

I have found a workaround, which is to manually specify an order_by for the mapper. Replacing the mapper creation in the test case with:
m=mapper(T, t, order_by=t.columns'id')
Makes the test work.

The test case also works if you don't have a schema on the table. It seems the main difference having a schema makes is that the table is aliased in the query. This triggers code in MSSQLCompiler.visit_column to alias the column as well. That works ok for the columns on the table, but the default_order_by column is a bit different; it is an oid column. FromClause.corresponding_column returns an object that is not part of the table, and this causes a KeyError in MSSQLCompiler.

Right now I'm not sure exactly which bit of this is at fault, but I will have a look at how some other databases with schemas handle this for some inspiration!


Attachments: bug-schema-minimal.py | alias-oid.patch

Activity

sqlalchemy-bot

sqlalchemy-bot commented on Feb 3, 2007

@sqlalchemy-bot
CollaboratorAuthor

paj (@paj) wrote:

minimal test case

sqlalchemy-bot

sqlalchemy-bot commented on Feb 3, 2007

@sqlalchemy-bot
CollaboratorAuthor

Changes by paj (@paj):

  • attached file bug-schema-minimal.py
sqlalchemy-bot

sqlalchemy-bot commented on Feb 3, 2007

@sqlalchemy-bot
CollaboratorAuthor

paj (@paj) wrote:

Hmmm, MSSQL is the only DB that considers schemas explicitly. The others just use the functionality in ansisql. I notice that simply removing visit_table, visit_alias and visit_column from MSSQLCompiler makes my simple test case work.

Why does MSSQL do the aliasing? If it's just a hangover from old days when ansisql didn't support schemas, I guess we can remove it.

sqlalchemy-bot

sqlalchemy-bot commented on Feb 6, 2007

@sqlalchemy-bot
CollaboratorAuthor

paj (@paj) wrote:

Rick Morrison explains: the aliasing is required by SQL server when performing
mixed-schema queries on SQL Server 2000. The table reflection queries
rely on this, so it can't be removed just yet.

sqlalchemy-bot

sqlalchemy-bot commented on Feb 6, 2007

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

(original author: ram) One possible workaround would be to set order_by to None if not set otherwise. This would break queries doing cross-schema selects that also depend on the default ordering - but that's a pretty small corner case.

sqlalchemy-bot

sqlalchemy-bot commented on Feb 13, 2007

@sqlalchemy-bot
CollaboratorAuthor

paj (@paj) wrote:

Ok, I think I have found the proper solution to this!

And it's frustratingly simple! Just a single line, and kind of obvious when you see it :-)

sqlalchemy-bot

sqlalchemy-bot commented on Feb 13, 2007

@sqlalchemy-bot
CollaboratorAuthor

paj (@paj) wrote:

Proposed fix

sqlalchemy-bot

sqlalchemy-bot commented on Feb 13, 2007

@sqlalchemy-bot
CollaboratorAuthor

Changes by paj (@paj):

  • attached file alias-oid.patch
sqlalchemy-bot

sqlalchemy-bot commented on Feb 14, 2007

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

(original author: ram) committed in rev 2315, thanks

sqlalchemy-bot

sqlalchemy-bot commented on Feb 14, 2007

@sqlalchemy-bot
CollaboratorAuthor

Changes by Anonymous:

  • changed status to closed
sqlalchemy-bot

sqlalchemy-bot commented on Apr 27, 2007

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

Milestone next micropoint release deleted

sqlalchemy-bot

sqlalchemy-bot commented on Apr 27, 2007

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • changed milestone from "next micropoint release" to "0.4.0"

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

    SQL ServerMicrosoft SQL Server, e.g. mssqlbugSomething isn't workinghigh priority

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sqlalchemy-bot

        Issue actions

          query.get() fails on tables with a schema · Issue #456 · sqlalchemy/sqlalchemy