Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_sa_skip_events error when invoking statement and calling SignallingSession #1066

Closed
softzer0 opened this issue Jul 16, 2022 · 2 comments
Closed
Milestone

Comments

@softzer0
Copy link

softzer0 commented Jul 16, 2022

Hello, I've got instructed to post here from the very same issue I've created on this place: sqlalchemy/sqlalchemy#8260

Here's the example code from here which I am using: https://github.com/sqlalchemy/sqlalchemy/blob/rel_1_4_39/test/ext/test_baked.py#L1023

When I try to execute a query with this event interceptor being in effect, I get the error about unexpected keyword argument _sa_skip_events, which I see is being appended to _bind_arguments in invoke_statement method, before calling execute: https://github.com/sqlalchemy/sqlalchemy/blob/rel_1_4_39/lib/sqlalchemy/orm/session.py#L221

Example code to reproduce:

from sqlalchemy.testing.fixtures import fixture_session
from sqlalchemy.orm.query import Query

class CachingQuery(Query):
    cache = {}

    def set_cache_key(self, key):
        return self.execution_options(_cache_key=key)

    def set_cache_key_for_path(self, path, key):
        return self.execution_options(**{"_cache_key_%s" % path: key})

def get_value(cache_key, cache, createfunc):
    if cache_key in cache:
        return cache[cache_key]()
    else:
        cache[cache_key] = retval = createfunc().freeze()
        return retval()

s1 = fixture_session(query_cls=CachingQuery)

@event.listens_for(s1, "do_orm_execute", retval=True)
def do_orm_execute(orm_context):
    ckey = None
    for opt in orm_context.user_defined_options:
        ckey = opt.get_cache_key(orm_context)
        if ckey:
            break
    else:
        if "_cache_key" in orm_context.execution_options:
            ckey = orm_context.execution_options["_cache_key"]

    if ckey is not None:
        return get_value(
            ckey,
            CachingQuery.cache,
            orm_context.invoke_statement,
        )

s1.query(User).filter(User.id == 7).set_cache_key("user7")

The error with the stack trace I'm getting:

File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 2896, in __iter__
  return self._iter().__iter__()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 2903, in _iter
  result = self.session.execute(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 1693, in execute
  result = fn(orm_exec_state)
File "/opt/project/app/project/__init__.py", line 63, in _do_orm_execute
  retval = orm_context.invoke_statement().freeze()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 233, in invoke_statement
  return self.session.execute(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 1700, in execute
  bind = self.get_bind(**bind_arguments)
TypeError: SignallingSession.get_bind() got an unexpected keyword argument '_sa_skip_events'

Environment:

  • Python version: 3.9
  • Flask-SQLAlchemy version: 2.5.1
  • SQLAlchemy version: 1.4.39
@softzer0
Copy link
Author

softzer0 commented Jul 16, 2022

It appears that in the latest official version (2.5.1) it's a bug: https://github.com/pallets-eco/flask-sqlalchemy/blob/2.5.1/flask_sqlalchemy/__init__.py#L187
While with the latest changes in this repository (main branch), it is already fixed with this commit: afd9af6

@davidism
Copy link
Member

Fixed in the linked commit, and addressed further in #1087.

@davidism davidism added this to the 3.0 milestone Sep 18, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants