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

Error when using polymorphic_on with CASE statement #297

Open
AbdealiLoKo opened this issue Aug 30, 2022 · 1 comment
Open

Error when using polymorphic_on with CASE statement #297

AbdealiLoKo opened this issue Aug 30, 2022 · 1 comment

Comments

@AbdealiLoKo
Copy link
Contributor

The polymorphic_on can be a CASE WHEN statement.
Ref: https://docs.sqlalchemy.org/en/14/orm/mapping_api.html#sqlalchemy.orm.mapper.params.polymorphic_on

But it looks like sqlalchemy-continuum fails when I try to add __versioned__ to a model with it.

  File "/home/abdealijk/venv/lib/python3.7/site-packages/sqlalchemy_continuum/builder.py", line 22, in check_reentry
    handler(*args, **kwargs)
  File "/home/abdealijk/venv/lib/python3.7/site-packages/sqlalchemy_continuum/builder.py", line 190, in configure_versioned_classes
    self.enable_active_history(pending_classes_copies)
  File "/home/abdealijk/venv/lib/python3.7/site-packages/sqlalchemy_continuum/builder.py", line 201, in enable_active_history
    getattr(cls, prop.key).impl.active_history = True
AttributeError: type object 'Writer' has no attribute '_sa_polymorphic_on'

Reproducible example:

from sqlalchemy import Column, Integer, String, case
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import configure_mappers
from sqlalchemy_continuum import make_versioned


make_versioned(user_cls=None)
Base = declarative_base()


class Writer(Base):
    __tablename__ = 'writer'
    __versioned__ = {}

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    type = Column(String(255))

    __mapper_args__ = {
        'polymorphic_on': (
            case(
                [(type.in_(['poet', 'lyricist']), 'bard')],
                else_=type,
            )
        ),
        'polymorphic_identity': 'writer',
    }


class Author(Base):
    __tablename__ = "author"
    __versioned__ = {}
    id = Column(Integer, primary_key=True)
    name = Column(String(255))

    __mapper_args__ = {
        'polymorphic_identity': 'author',
    }


configure_mappers()
@AbdealiLoKo
Copy link
Contributor Author

A alternate syntax here is:

class Writer(Base):
    ...
    poly_col = column_property(
        case(
            [(type.in_(['poet', 'lyricist']), 'bard')],
            else_=type,
        )
    )

    __mapper_args__ = {
        'polymorphic_on': poly_col,
        'polymorphic_identity': 'writer',
    }

But this seems to be failing with the error that the column property is not found in the version table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant