Concrete inheritance with different hybrid methods

19 views
Skip to first unread message

Damian Yurzola

unread,
Jan 7, 2020, 1:38:34 PM1/7/20
to sqlalchemy
Folks I have this toy example I've sanitized.

class TableBase(AbstractConcreteBase, Base):
    pass


class TableA(TableBase):
    __tablename__ = "table_a"
    __mapper_args__ = {
        "polymorphic_identity": "A",
        "concrete": True,
    }

    v0 = Column(Integer)

    @hybrid_property
    def magic(self):
        return self.v0 * 100

    @magic.expression
    def magic(cls):
        return cls.v0 * 100


class TableB(TableBase):
    __tablename__ = "table_b"
    __mapper_args__ = {
        "polymorphic_identity": "B",
        "concrete": True,
    }
    v0 = Column(Integer)

    @hybrid_property
    def magic(self):
        return self.v0 * 10

    @magic.expression
    def magic(cls):
        return cls.v0 * 10


When I query at the top level:

print(Query([TableBase.magic]).limit(1).statement)

I get

AttributeError: type object 'TableBase' has no attribute 'magic'


I've tried a bunch of things, most of them point to the fact that the hybrid_property is not being rendered in the subquery.
I did solve it by moving the hybrid_property into the abstract class, though I want the particularities of a table to stay with the table definition and not at the union level.

Thoughts? Ideas?

Thanks!!!

--Damian

Mike Bayer

unread,
Jan 7, 2020, 8:53:19 PM1/7/20
to noreply-spamdigest via sqlalchemy
to accomplish this you would need to rewrite the orm.util.polymorphic_union function to include these additional expressions within each SELECT under a common label name, then override the part of AbstractConcreteBase which applies the polymorphic_union in order to accomplish this.

These two functions could be enhanced to support additional expressions added to the polymorphic_union however that's not available right now.

Otherwise, you need to select from TableA and TableB explicitly and write the UNION directly.

if this in fact a "toy" example then I would say you're better off not using concrete inheritance as it is not very fluent.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Damian Yurzola

unread,
Jan 8, 2020, 12:25:22 PM1/8/20
to sqlalchemy
Thanks Mike. This is good to know.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages