Полиморфная фильтрация по гибридным свойствам при наследовании от AbstractConcreteBase - PullRequest
0 голосов
/ 26 февраля 2019

Я успешно полиморфно загружаю элементы через AbstractConcreteBase родительский элемент.Можно ли также фильтровать по hybrid_property определениям потомков?Я также хочу фильтровать разные столбцы для каждого дочернего класса.

Запросы

    # Fetching all ItemA and ItemB works well with...
    ItemBase.query.all()

    # Given the models below is it possible to filter on the children's
    # item_id hybrid_property to fetch all ItemB with item_b_id of 1
    # Result is []
    ItemBase.query.filter(ItemBase.item_id == 'B1')

    # This also doesn't work
    # Result is everything unfiltered
    ItemBase.query.filter(ItemB.item_id == 'B1')

Модели:

from sqlalchemy.sql.expression import cast
from sqlalchemy.ext.declarative import AbstractConcreteBase
from sqlalchemy.ext.hybrid import hybrid_property

class ItemBase(AbstractConcreteBase, Base):
    __tablename__ = None

    @hybrid_property
    def item_id(self): pass

    @activity_id.expression
    def item_id(cls):
       pass

class ItemA(ItemBase):
    __tablename__ = 'item_a'

    __mapper_args__ = {
        'polymorphic_identity': 'item_a',
        'concrete':True
    }

    item_a_id = db.Column(db.Integer, primary_key=True)

    @hybrid_property
    def item_id(self):
        return 'A' + str(self.item_a_id)

    @activity_id.expression
    def item_id(cls): 
        return 'A' + str(self.item_a_id)

class ItemB(ItemBase):
    __tablename__ = 'item_b'

    __mapper_args__ = {
        'polymorphic_identity': 'item_b',
        'concrete':True
    }

    item_b_id = db.Column(db.Integer, primary_key=True)

    @hybrid_property
    def item_id(self):
        return 'B' + str(self.item_b_id)

    @activity_id.expression
    def item_id(cls): 
        return 'B' + str(self.item_b_id)

Пока я застрял со структурой таблицы.Любая помощь с благодарностью.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...