Andrii
Andrii

Reputation: 324

InvalidRequestError: Could not evaluate current criteria in Python: "Cannot evaluate Select". Specify 'fetch' or False for the synchronize_session

have this error when try run this code:

query = delete(BannerLocalization).where(
        BannerLocalization.locale == locale,
        BannerLocalization.id == pk,
        BannerLocalization.banner_id.in_(
            select(
                Banner.id
            ).join(
                BannerLocalization
            ).where(
                BannerLocalization.locale == locale,
                BannerLocalization.id == pk,
                Banner.banner_type == BannerTypesEnum.EVENT
            )
        )
    )

sqlalchemy.exc.InvalidRequestError: Could not evaluate current criteria in Python: "Cannot evaluate Select". Specify 'fetch' or False for the synchronize_session execution option.

Does somebody know what is wrong?

Upvotes: 8

Views: 7504

Answers (1)

Andrii
Andrii

Reputation: 324

I've resolves this problem:

Set execution_options=immutabledict({"synchronize_session": 'fetch'}) to session.execute

Working variant looks like this:

from sqlalchemy.util import immutabledict

await session.execute(
    query, 
    execution_options=immutabledict(
        {"synchronize_session": 'fetch'}
    )
)

Upvotes: 10

Related Questions