[Python] Using polymorphic_identity and inheritance is raising a warning

Discussão em 'Python' iniciado por Stack, Julho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code, where I need to inherit GroupAB from GroupA because I want to use its functions vs copy-paste them again. since I can't use multiple values for polymorphic_identity=["GroupA", "GroupAB"] I tried using the below code yet it resulted with a warning

    The main group is:

    class Group(Base):
    id = Column('id', Integer, primary_key=True)
    name = Column('name', String(15))
    g_type = Column('type', String(15))

    __mapper_args__ = {
    'polymorphic_on': g_type,
    'polymorphic_identity': 'Group'
    }


    What I wished for:

    class GroupCommon(Group):
    __mapper_args__ = {
    'polymorphic_identity': ['GroupA', 'GroupAB']
    }

    def some_function(self}:
    ....

    # not possible, it doesn't accept a list - and raise:
    if self.polymorphic_identity in self.polymorphic_map:
    TypeError: unhashable type: 'list'


    What I tried to do- is to inherit them:

    class GroupA(Group):
    __mapper_args__ = {
    'polymorphic_identity': 'GroupA'
    }

    def some_function(self}:
    ....

    class GroupAB(GroupA): <= Inherit from GroupA which is a polymorphic_identity of Group
    __mapper_args__ = {
    'polymorphic_identity': 'GroupAB'
    }

    pass # nothing here as i'm using GroupA's functions

    # -- this works -- but raise a warning
    SAWarning: Flushing object <GroupAB at 0x1082...> with incompatible polymorphic identity 'GroupAB'; the object may not refresh and/or load correctly
    (this warning may be suppressed after 10 occurrences)


    No warning when I use:

    class GroupAB(Group): <= Inherit from Group, no functions available ofc
    __mapper_args__ = {
    'polymorphic_identity': 'GroupAB'
    }

    pass


    Any thoughts ?

    Continue reading...

Compartilhe esta Página