Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load subset of joined columns in SQLAlchemy

I'm trying to load a subset of columns from multiple joined tables in SQLAlchemy and can't figure out the magic syntax.

Here's a simple example of what I'm trying to do. It results in an error (below):

from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, load_only

Base = declarative_base()

class Table1(Base):
    __tablename__ = 'table1'
    table1_id = Column(Integer, primary_key=True)
    table1_val = Column(String)
    r1 = relationship('Table2', backref = 'r2')


class Table2(Base):
    __tablename__ = 'table2'
    table2_id = Column(Integer, ForeignKey('table1.table1_id'), primary_key=True)
    table2_val = Column(String)


from sqlalchemy.orm import sessionmaker
some_engine = create_engine('postgresql://scott:tiger@localhost/')
Session = sessionmaker(bind=some_engine)
session = Session()
query = session.query(Table1).join(Table2).options(load_only('table1_val','table2_val'))

ArgumentError: Can't find property named 'table2_val' on the mapped entity Mapper|Table1|table1 in this Query.

How do I pick and choose columns from these multiple tables?

like image 807
John Prior Avatar asked Mar 13 '16 05:03

John Prior


1 Answers

Question was answered at [https://bitbucket.org/zzzeek/sqlalchemy/issues/3679/multiple-load_only-in-queryoptions-ignore]

Here is the corrected code:

from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import joinedload, Load, relationship, load_only

Base = declarative_base()

class Table1(Base):
    __tablename__ = 'table1'
    global_id = Column(Integer, primary_key=True)
    table1_val1 = Column(String)
    table1_val2 = Column(String)
    r1 = relationship('Table2', backref = 'r2')

class Table2(Base):
    __tablename__ = 'table2'
    global_id = Column(Integer, ForeignKey('table1.global_id'), primary_key=True)
    table2_val1 = Column(String)
    table2_val2 = Column(String)

from sqlalchemy.orm import sessionmaker
some_engine = create_engine('sqlite://')
Base.metadata.create_all(some_engine)
Session = sessionmaker(bind=some_engine)
session = Session()

session.add(Table1(table1_val1='1val1',table1_val2='1val2',r1=[Table2(table2_val1='2val1', table2_val2='2val2')]))
session.commit() # expires the attribute from the session

query = session.query(Table1).options(
    # note that the 'load_only()' is applied to the 'joinedload' path, not 
    # put on its own Load() path
    joinedload('r1',innerjoin=True).load_only('table2_val1'),
    Load(Table1).load_only('table1_val1'))

foo = query.all()
assert 'table1_val2' not in foo[0].__dict__
assert 'table2_val2' not in foo[0].r1[0].__dict__
like image 125
John Prior Avatar answered Oct 21 '22 08:10

John Prior