here's a full MCVE, which with "flat=True" reproduces your issue
exactly (send me this next time).   The issue is the "flat=True", take
that out because it isn't compatible with a concrete union - it
implies the "aliased" flag and that gets in the way of what the
polymorphic_union() function is doing already.


from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.ext.declarative import AbstractConcreteBase

Base = declarative_base()


class OrderItem(AbstractConcreteBase, Base):
    pass


class OrderTestItem(OrderItem):
    __tablename__ = 'order_test_item'

    order_id = Column(Integer, ForeignKey("order.id"), primary_key=True)
    test_id = Column(Integer, ForeignKey("test.id"), primary_key=True)

    test = relationship('Test')
    order = relationship('Order')

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


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)


class Test(Base):
    __tablename__ = 'test'
    id = Column(Integer, primary_key=True)


class Order(Base):
    __tablename__ = 'order'

    id = Column(Integer, primary_key=True)

    user_id = Column(Integer, ForeignKey('user.id'))

    user = relationship('User')
    items = relationship('OrderItem')

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

s = Session(e)

t1, t2 = Test(), Test()
s.add(Order(items=[OrderTestItem(test=t1), OrderTestItem(test=t2)]))
s.commit()


pjoin = polymorphic_union({
    'order_test_item': OrderTestItem.__table__,
}, 'type', 'pjoin')

order_items = with_polymorphic(
    OrderItem, [OrderTestItem], selectable=pjoin)


s = Session(e)
order = s.query(Order).join(Order.items.of_type(order_items)).all()




On Wed, Mar 7, 2018 at 3:36 PM, Harshvardhan Gupta
<harshsay...@gmail.com> wrote:
> Thanks for replying.
> I forgot to show the error.
> It is this:
>
> 1054, "Unknown column 'pjoin.order_id' in 'on clause'") [SQL: "SELECT
> `order`.id AS order_id, `order`.rp_id AS order_rp_id, `order`.user_id AS
> order_user_id \nFROM `order` INNER JOIN (SELECT order_test_item.order_id AS
> order_id, order_test_item.test_id AS test_id, 'order_test_item' AS type
> \nFROM order_test_item) AS anon_1 ON `order`.id = pjoin.order_id"
>
>
>
>
> On Wednesday, 7 March 2018 15:28:54 UTC-5, Mike Bayer wrote:
>>
>> On Tue, Mar 6, 2018 at 8:36 PM, Harshvardhan Gupta
>> <harsh...@gmail.com> wrote:
>> > I tried to use AbstractConcreteBase for polymorphic relationships , but
>> > I am
>> > getting errors. The examples in sqlalchemy cover normal polymorphism
>> > well,
>> > but not those with Abstract Base classes.
>> >
>> >
>> > I have already asked a question on stack overflow .
>> >
>> > The gist of the question is:
>> >
>> > class OrderItem(Dictifiable, AbstractConcreteBase, db.Model):
>> >     pass
>> >
>> >
>> > class OrderTestItem(OrderItem):
>> >     order_id = Column(Integer, ForeignKey("order.id"), primary_key=True)
>> >     test_id = Column(Integer, ForeignKey("test.id"), primary_key=True)
>> >
>> >     test = relationship('Test')
>> >     order = relationship('Order')
>> >
>> >     __mapper_args__ = {
>> >         'polymorphic_identity': 'order_test_item',
>> >         'concrete': True
>> >     }
>> >
>> >
>> >
>> > class Order(Dictifiable, db.Model): # This class has a relation to the
>> > polymorphic class
>> >
>> >     id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
>> >
>> >     user_id = Column(Integer, ForeignKey('user.id'))
>> >
>> >     user = relationship('User')
>> >     items = relationship('OrderItem')
>> >
>> >
>> > I query like :
>> >
>> > pjoin = polymorphic_union({
>> >     'order_test_item': OrderTestItem.__table__,
>> > }, 'type', 'pjoin')
>> >
>> > order_items =
>> > with_polymorphic(OrderItem,[OrderTestItem],selectable=pjoin,flat=True)
>> >
>> > And my actual query :
>> >
>> > order = Order.query.join(Order.items.of_type(order_items)).all()
>> >
>> >
>> > I would like to know what the correct way to query these tables is, how
>> > to
>> > eager load polymorphic tables, and how to filter on the relationships.
>>
>> looks correct to me.  what is "the error" ?   can you provide full
>> MCVE + complete stack trace?
>>
>>
>>
>> >
>> > I plan to send a pull request with an example of these test cases after
>> > I
>> > know the answers myself.
>> >
>> >
>> > --
>> > SQLAlchemy -
>> > The Python SQL Toolkit and Object Relational Mapper
>> >
>> > http://www.sqlalchemy.org/
>> >
>> > 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.
>> > To post to this group, send email to sqlal...@googlegroups.com.
>> > Visit this group at https://groups.google.com/group/sqlalchemy.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to