<p dir="auto"><strong>Migrated issue, originally created by jvanasco (<a href="https://github.com/jvanasco">@jvanasco</a>)</strong></p>
<p dir="auto">example attached below; this happens in 0.9.4</p>
<p dir="auto">if trying to load_only a column of the child relationship and not passing in the primary_key, sqlalchemy isn't happy</p>
<p dir="auto">the sql generated is fine and runs -- it pulls the specified column and uses query string as an anonymous column ( sidenote: is that really necessary ?)</p>
<p dir="auto">but the ORM is unhappy and raises an error.</p>
<p dir="auto">adding the primary key works.</p>
<p dir="auto">using a joinedload instead of subqueryload works as well.</p>
<p dir="auto">and hey, i made a proper test case!</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="CATCH_KNOWN_EXCEPTION = False

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy.exc

Base = declarative_base()    

class TableA(Base):
    __tablename__ = 'table_a'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))

    bs = relationship(&quot;TableB&quot;, primaryjoin=&quot;TableB.table_a_id==TableA.id&quot;)
    cs = relationship(&quot;TableC&quot;, primaryjoin=&quot;TableC.table_a_id==TableA.id&quot;)

class TableB(Base):
    __tablename__ = 'table_b'
    id = Column(Integer, primary_key=True)
    name = Column(String(30))
    table_a_id = Column(Integer, ForeignKey('table_a.id'), nullable=False)

class TableC(Base):
    __tablename__ = 'table_c'
    id = Column(Integer, primary_key=True)
    id_alt = Column(Integer, primary_key=True)
    name = Column(String(30))
    table_a_id = Column(Integer, ForeignKey('table_a.id'), nullable=False)


e = create_engine(&quot;sqlite://&quot;, echo=True)
Base.metadata.create_all(e)

s = Session(e)

s.add_all([
    TableA(id=1, name='a',),
    TableA(id=2, name='aa',),
    TableA(id=3, name='aaa',),
    TableA(id=4, name='aaaa',),
    TableB(id=1, name='b', table_a_id=1),
    TableB(id=2, name='bb', table_a_id=2),
    TableB(id=3, name='bbb', table_a_id=3),
    TableB(id=4, name='bbbbb', table_a_id=4),
    TableC(id=1, id_alt=10, name='c', table_a_id=1),
    TableC(id=2, id_alt=11, name='cc', table_a_id=2),
    TableC(id=3, id_alt=12, name='ccc', table_a_id=3),
    TableC(id=4, id_alt=13, name='cccc', table_a_id=4),
])

s.commit()


q = s.query( TableA ).filter( TableA.id == 2 )

## this passes
try:
    q_good = q.options( subqueryload('bs').load_only('id', 'name') )
    result = q_good.first()
    print &quot;expected - can load `bs` with primary_key + desired column&quot;
except:
    raise

try: 
    q_bad = q.options( subqueryload('bs').load_only('name') )
    result = q_bad.first()
except sqlalchemy.exc.NoSuchColumnError, e:
    print &quot;expected - CAN NOT LOAD `bs` with only desired column&quot;
    if not CATCH_KNOWN_EXCEPTION:
        raise

try:
    joinedload_works = q.options( joinedload('bs').load_only('name') )
    result = joinedload_works.first()
    print &quot;expected - can load `bs` with joinedload only using the target column&quot;
except:
    raise

try: 
    q_bad = q.options( subqueryload('cs').load_only('name') )
    result = q_bad.first()
except sqlalchemy.exc.NoSuchColumnError, e:
    print &quot;expected - CAN NOT LOAD `cs` with only desired column&quot;
    if not CATCH_KNOWN_EXCEPTION:
        raise

## this also fails, only one key
try: 
    q_bad = q.options( subqueryload('cs').load_only('id', 'name') )
    result = q_bad.first()
except sqlalchemy.exc.NoSuchColumnError, e:
    print &quot;expected - CAN NOT LOAD `cs` with only 1/2 primary keys + desired column&quot;
    if not CATCH_KNOWN_EXCEPTION:
        raise

try:
    q_good = q.options( subqueryload('cs').load_only('id', 'id_alt', 'name'))
    result = q_good.first()
    print &quot;expected - can load `cs` with subqueryload using the target column + BOTH primary keys&quot;
except:
    raise



s.close()"><pre class="notranslate"><code class="notranslate">CATCH_KNOWN_EXCEPTION = False

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy.exc

Base = declarative_base()    

class TableA(Base):
    __tablename__ = 'table_a'
    id = Column(Integer, primary_key=True)
    name = Column(String(50))

    bs = relationship("TableB", primaryjoin="TableB.table_a_id==TableA.id")
    cs = relationship("TableC", primaryjoin="TableC.table_a_id==TableA.id")

class TableB(Base):
    __tablename__ = 'table_b'
    id = Column(Integer, primary_key=True)
    name = Column(String(30))
    table_a_id = Column(Integer, ForeignKey('table_a.id'), nullable=False)

class TableC(Base):
    __tablename__ = 'table_c'
    id = Column(Integer, primary_key=True)
    id_alt = Column(Integer, primary_key=True)
    name = Column(String(30))
    table_a_id = Column(Integer, ForeignKey('table_a.id'), nullable=False)


e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

s = Session(e)

s.add_all([
    TableA(id=1, name='a',),
    TableA(id=2, name='aa',),
    TableA(id=3, name='aaa',),
    TableA(id=4, name='aaaa',),
    TableB(id=1, name='b', table_a_id=1),
    TableB(id=2, name='bb', table_a_id=2),
    TableB(id=3, name='bbb', table_a_id=3),
    TableB(id=4, name='bbbbb', table_a_id=4),
    TableC(id=1, id_alt=10, name='c', table_a_id=1),
    TableC(id=2, id_alt=11, name='cc', table_a_id=2),
    TableC(id=3, id_alt=12, name='ccc', table_a_id=3),
    TableC(id=4, id_alt=13, name='cccc', table_a_id=4),
])

s.commit()


q = s.query( TableA ).filter( TableA.id == 2 )

## this passes
try:
    q_good = q.options( subqueryload('bs').load_only('id', 'name') )
    result = q_good.first()
    print "expected - can load `bs` with primary_key + desired column"
except:
    raise

try: 
    q_bad = q.options( subqueryload('bs').load_only('name') )
    result = q_bad.first()
except sqlalchemy.exc.NoSuchColumnError, e:
    print "expected - CAN NOT LOAD `bs` with only desired column"
    if not CATCH_KNOWN_EXCEPTION:
        raise

try:
    joinedload_works = q.options( joinedload('bs').load_only('name') )
    result = joinedload_works.first()
    print "expected - can load `bs` with joinedload only using the target column"
except:
    raise

try: 
    q_bad = q.options( subqueryload('cs').load_only('name') )
    result = q_bad.first()
except sqlalchemy.exc.NoSuchColumnError, e:
    print "expected - CAN NOT LOAD `cs` with only desired column"
    if not CATCH_KNOWN_EXCEPTION:
        raise

## this also fails, only one key
try: 
    q_bad = q.options( subqueryload('cs').load_only('id', 'name') )
    result = q_bad.first()
except sqlalchemy.exc.NoSuchColumnError, e:
    print "expected - CAN NOT LOAD `cs` with only 1/2 primary keys + desired column"
    if not CATCH_KNOWN_EXCEPTION:
        raise

try:
    q_good = q.options( subqueryload('cs').load_only('id', 'id_alt', 'name'))
    result = q_good.first()
    print "expected - can load `cs` with subqueryload using the target column + BOTH primary keys"
except:
    raise



s.close()
</code></pre></div>
<p dir="auto"><strong>Migrated issue, originally created by Ігор Горобець</strong></p>
<p dir="auto">Hello!</p>
<p dir="auto">How about integrate it?<br>
Is it even possible?</p>