flipbit03 / sqlalchemy-easy-softdelete

Easily add soft-deletion to your SQLAlchemy Models
Other
62 stars 13 forks source link

Add support for .union() using SQLAlchemy Core #12

Closed flipbit03 closed 1 year ago

flipbit03 commented 1 year ago

Description

Library does not support UNION queries generated using SQLAlchemy Core Statements

What I Did

I've created a query using SQLAlchemy Core statements which has a .union() on it, and the library failed with the following error:

Query:

    user_and_org_select = (
        select([user_table.c.id, text("user"), account_name_table.c.name, account_name_table.c.normalized_name])
        .select_from(user_table)
        .outerjoin(account_name_table, user_table.c.id == account_name_table.c.user_id)
    ).union(
        select([organization_table.c.id, text("organization"), account_name_table.c.name,
                account_name_table.c.normalized_name])
        .select_from(organization_table)
        .outerjoin(account_name_table, organization_table.c.id == account_name_table.c.organization_id)
    )

error

  File "/home/cadu/.local/share/virtualenvs/kernel-backend-N3YlRYFF/lib/python3.11/site-packages/sqlalchemy_easy_softdelete/handler/sqlalchemy_easy_softdelete.py", line 20, in soft_delete_execute
    adapted = SoftDeleteQueryRewriter(deleted_field_name, disable_soft_delete_option_name).rewrite_statement(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cadu/.local/share/virtualenvs/kernel-backend-N3YlRYFF/lib/python3.11/site-packages/sqlalchemy_easy_softdelete/handler/rewriter/__init__.py", line 47, in rewrite_statement
    raise NotImplementedError(f"Unsupported statement type \"{(type(stmt))}\"!")
NotImplementedError: Unsupported statement type "<class 'sqlalchemy.sql.selectable.CompoundSelect'>"!