Skip to content

more session arguments not being sent in places they are needed #7837

Closed

Description

@zzzeek
  1. add bind_arguments to session.get()
  2. propagate execution_options, bind_arguments get() arguments to _get_impl(), then to _identity_lookup, it's very strange that bind_arguments aren't passed here
  3. add new horiziontal sharding hook identity_chooser(), pass to this chooser: mapper, exec_options, bind_arguments, primary key, identity_token, lazy_loaded_from, **kw
  4. deprecate id_chooser the same way query_chooser is deprecated
  5. expose "identity_token" as a setter/getter on ORMExecuteState, to better help with sharding types of use cases that dont necessarily use horizontal sharding (simplify HS API to use this attribute also?) see example below
  6. add execution_options to Session constructor, to set up default execution args

update sharding examples to use above new APIs

example of identity_token API

import sqlalchemy as sa
from sqlalchemy import event
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class MyTable(Base):
    __tablename__ = "example"

    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.Text)


engine: sa.engine.Engine = sa.create_engine(
    "postgresql://scott:tiger@localhost/test",
    echo="debug",
)

with engine.begin() as conn:
    Base.metadata.drop_all(
        conn.execution_options(schema_translate_map={None: "test_schema"})
    )
    Base.metadata.drop_all(
        conn.execution_options(schema_translate_map={None: "test_schema_2"})
    )

    Base.metadata.create_all(
        conn.execution_options(schema_translate_map={None: "test_schema"})
    )
    Base.metadata.create_all(
        conn.execution_options(schema_translate_map={None: "test_schema_2"})
    )


map1 = {None: "test_schema"}
map2 = {None: "test_schema_2"}

with Session(engine.execution_options(schema_translate_map=map1)) as sess:
    sess.add(MyTable(name="this is schema one"))
    sess.commit()

with Session(engine.execution_options(schema_translate_map=map2)) as sess:
    sess.add(MyTable(name="this is schema two"))
    sess.commit()


with Session(engine) as sess:

    @event.listens_for(sess, "do_orm_execute")
    def do_orm_execute(context):
        desired_schema = context.execution_options.get("desired_schema", None)
        if desired_schema:
            context.update_execution_options(
                schema_translate_map={None: desired_schema},

                # this part is ugly, so ORMExecuteState may be added
                # an identity_token attribute that you can set here
                _sa_orm_load_options=context.load_options
                + {"_refresh_identity_token": desired_schema},
            )

    base_qry = sess.query(MyTable).filter(MyTable.id == 1)

    obj1 = base_qry.execution_options(desired_schema="test_schema").one()

    assert obj1.name == "this is schema one"

    obj2 = base_qry.execution_options(desired_schema="test_schema_2").one()

    assert obj2.name == "this is schema two"

    assert obj1 is not obj2

Activity

  1. added this to the 2.0 milestone on Mar 21, 2022
  2. modified the milestones: 2.0, 2.0beta on Oct 11, 2022
  3. sqla-tester commented on Dec 18, 2022

    @sqla-tester
    Collaborator

    Mike Bayer has proposed a fix for this issue in the main branch:

    re-consume execute options in orm_setup_cursor_result? https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4313

  4. zzzeek commented on Dec 20, 2022

    @zzzeek
    MemberAuthor

    so I have this in progress but now I'm not sure why.

    expose "identity_token" as a setter/getter on ORMExecuteState, to better help with sharding types of use cases that dont necessarily use horizontal sharding (simplify HS API to use this attribute also?) see example below

    why ? how is this useful if they aren't also setting "connection callable" that works in the flush process? this issue has no link to whatever discussion it came from.

  5. zzzeek commented on Dec 20, 2022

    @zzzeek
    MemberAuthor

    OK the use case is #7832. it's read-only, or at least has to be documented as such. we can put that in the example

  6. zzzeek commented on Dec 21, 2022

    @zzzeek
    MemberAuthor

    add new horiziontal sharding hook identity_chooser(), pass to this chooser: mapper, exec_options, bind_arguments, primary key, identity_token, lazy_loaded_from, **kw

    by definition, when we call identity_chooser(), we dont have a non-None identity token. that's literally the thing we are trying to determine! So remove this from the new hook

  7. modified the milestones: 2.0 final, 2.0beta5 on Dec 21, 2022
  8. zzzeek commented on Dec 22, 2022

    @zzzeek
    MemberAuthor

    add execution_options to Session constructor, to set up default execution args

    going to leave this part out. still not ready to do this. "autoflush" being in two places for example seems inconsistent.

  9. added a commit that references this issue on Jun 9, 2025
    4338213
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions