sqlalchemy / sqlalchemy/sqlalchemy

support setting load options on instances

Open
#3,037 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature loader options orm
Dominant language
Python
Stars
12.2k
Forks
1.8k
PR merge metrics
No merged PRs in 30d

Description

Migrated issue, originally created by Michael Bayer (@zzzeek)

proof of concept

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

Base = declarative_base()

class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    bs = relationship("B")

class B(Base):
    __tablename__ = 'b'

    id = Column(Integer, primary_key=True)
    a_id = Column(Integer, ForeignKey('a.id'))
    cs = relationship("C")

class C(Base):
    __tablename__ = 'c'

    id = Column(Integer, primary_key=True)
    b_id = Column(Integer, ForeignKey('b.id'))

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

sess = Session(e)
sess.add_all([
    A(bs=[
        B(cs=[C(), C()]),
        B(cs=[C(), C()])
    ])
])
sess.commit()

a1 = sess.query(A).first()
inspect(a1).load_options = inspect(a1).load_options.union([joinedload(B.cs)])

for b in a1.bs:
    print b.cs

im not sure why the above option doesn't have to mention "A.bs" at all, there should be some path logic taking this into account. The public API should be:

inspect(a1).set_load_option("bs", joinedload(...))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shown inspect(a1).load_options proof of concept and the proposed inspect(a1).set_load_option API; trace how the instance and relationship path are represented. Done means a supported instance-level load option can target the intended relationship path, with behavior matching the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlalchemy
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.