There's examples of custom SQL expressions at:

http://docs.sqlalchemy.org/en/latest/orm/mapping_api.html#sqlalchemy.orm.mapper.params.polymorphic_on

These examples don't currently illustrate a subquery (which would be a good idea), but the idea is you make a scalar correlated subquery (a Core example is at http://docs.sqlalchemy.org/en/latest/core/tutorial.html#correlated-subqueries) and you can stick that right into polymorphic_on, or send it as a column_property.


class Publication(db.Model):
    __tablename__ = 'publication'
    id = db.Column(db.Integer, primary_key=True)
series_id = db.Column(db.Integer, db.ForeignKey('series.id'), nullable=False)
    series = db.relationship(Series, backref='publications')
    __mapper_args__ = {
'polymorphic_on': select([series.type]).where(series_id == Series.id).as_scalar(),
        'polymorphic_identity': None
    }




On 01/27/2017 06:20 PM, Shane Carey wrote:
Hi, I want to use a discriminator based upon a column of a related table.

I found the relevant place in the
docs 
http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#joined-table-inheritance

"The discriminator column is only needed if polymorphic loading is
desired, as is usually the case. It is not strictly necessary that it be
present directly on the base mapped table, and can instead be defined on
a derived select statement that’s used when the class is queried;
however, this is a much more sophisticated configuration scenario"

However this is not working for me, additionally I found this
stackoverflow question which also encounters this problem to a
tee. 
http://stackoverflow.com/questions/40862634/sqlalchemy-orm-polymorphic-on-from-relationship

What is the correct method to have a discriminator based on a more
complex select statement? Thank you!

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

Reply via email to