it doesn't produce as efficient of a SQL query, and requires that you set 
"Type" manually on the object, but you can use a correlated subquery using this 
form:

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

Base = declarative_base()

class Type(Base):
    __tablename__ = 'type'
    id = Column(Integer, primary_key=True)
    name = Column(String)

class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    type_id = Column(Integer, ForeignKey('type.id'), nullable=False)

    type = relationship("Type")

    type_name = column_property(select([Type.name]).where(Type.id == 
type_id).as_scalar())
    __mapper_args__ = {'polymorphic_identity': 'a', 'polymorphic_on': type_name}

class SubA(A):
    __tablename__ = 'sub_a'

    id = Column(Integer, ForeignKey('a.id'), primary_key=True)

    __mapper_args__ = {'polymorphic_identity': 'sub_a'}

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

s = Session(e)
ta, tsuba = Type(name='a'), Type(name='sub_a')
s.add_all([ta, tsuba])

s1 = SubA(type=tsuba)
s2 = A(type=ta)
s.add_all([s1, s2])
s.commit()

s.close()

print s.query(A).filter_by(id=1).one()

the other way to go is just to have polymorphic_on be "type_id" and use the 
integer identifiers as the polymorphic identities.


On Jan 28, 2013, at 7:52 PM, Ben Hitz wrote:

> Can I use a field from a related table as the polymorphic descriminator?
> 
> This code no works:
> http://pastebin.com/LtRC2tSR
> 
> Schematically:
> class Reference:
>     reference_id
>     reference_name
> 
> class Parent:
>     parent_id
>     reference_id(ForeignKey='reference.reference_id')
> 
>     reference = relationship(Reference, useList=False)
> 
>     __mapper_args__ {
>         polymorphic_on: reference.reference_name
>     }
> 
> class Child
>     child_id(ForeignKey='parent.parent_id')
>     child_property
> 
>     __mapper_args__ {
>         polymorphic_identity: "some value of reference.name"
>        }
> 
> I also tried an association_proxy but I get sqlalchemy.exc.ArgumentError: 
> Only direct column-mapped property or SQL expression can be passed for 
> polymorphic_on
> 
> I guess there is someway to extract the SQL emitted by the relationship and 
> use that as the value of polymorphic_on?  Or do I have to use something like:
> 
> select([Reference.reference_name].join(Parent)
> 
> Thanks
> Ben
> 
> 
> -- 
> 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 http://groups.google.com/group/sqlalchemy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

-- 
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 http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to