[Sqlalchemy-tickets] Issue #3080: load_only triggers `sqlalchemy.exc.NoSuchColumnError` if primary
Brought to you by:
zzzeek
|
From: jvanasco <iss...@bi...> - 2014-06-11 23:25:23
|
New issue 3080: load_only triggers `sqlalchemy.exc.NoSuchColumnError` if primary key is not included on subqueryload https://bitbucket.org/zzzeek/sqlalchemy/issue/3080/load_only-triggers jvanasco: example attached below; this happens in 0.9.4 if trying to load_only a column of the child relationship and not passing in the primary_key, sqlalchemy isn't happy 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 ?) but the ORM is unhappy and raises an error. adding the primary key works. using a joinedload instead of subqueryload works as well. and hey, i made a proper test case! from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base 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") 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) 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='aa', table_a_id=2), TableB(id=3, name='aaa', table_a_id=3), TableB(id=4, name='aaaa', table_a_id=4), ]) s.commit() q = s.query( TableA ).filter( TableA.id == 2 ) ## this passes q_good = q.options( subqueryload('bs').load_only('id','name') ) print "trying q_good" result = q_good.first() q_bad = q.options( subqueryload('bs').load_only('name') ) print "trying q_bad" result = q_bad.first() s.close() |