sqlite join rewriting fails on columns that assign alternate .key

Issue #2790 resolved
rgg created an issue

Here's a simple test, however something is also going on with an aliased version which only appears during the mechanics used by joinedload, need to isolate that:

from sqlalchemy import *
from sqlalchemy.orm import *

metadata = MetaData()

a = Table('a', metadata,
        Column('id', Integer, primary_key=True, key='aid'),
    )

b = Table('b', metadata,
        Column('id', Integer, primary_key=True, key='bid'),
    )
c = Table('c', metadata,
        Column('aid', Integer, ForeignKey('a.aid')),
        Column('bid', Integer, ForeignKey('b.bid')),
    )

from sqlalchemy.dialects import sqlite

s1 = select([b](a,), use_labels=True).select_from(a.join(c.join(b)))

print s1.compile(dialect=sqlite.dialect())


ca = c.alias()
ba = b.alias()

j = ca.join(ba)

# with aliases, need to list out the column w the key
s1 = select([ba.c.bid](a,), use_labels=True).select_from(a.join(j))

print s1.compile(dialect=sqlite.dialect())

Comments (4)

  1. Mike Bayer repo owner

    OK well here's the issue:

    diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
    index a5f545d..e535247 100644
    --- a/lib/sqlalchemy/sql/compiler.py
    +++ b/lib/sqlalchemy/sql/compiler.py
    @@ -1162,7 +1162,7 @@ class SQLCompiler(engine.Compiled):
                                         use_labels=True).alias()
    
                     for c in selectable.c:
    -                    c._label = c._key_label = c.name
    +                    c._label = c._key_label = c.key
                     translate_dict = dict(
                             zip(right.element.c, selectable.c)
                         )
    

    want to figure out that eager version though

  2. Log in to comment