this is not totally public API but should work:
from sqlalchemy.orm import class_mapper
mapper = class_mapper(MySubClass)
mapper.polymorphic_identity = "some_identity"
mapper.polymorphic_map["some_identity"] = mapper
# in 0.8, might try to fix this for 0.9
mapper._identity_class = mapper.inherits._identity_class
this is something that can be supported as a feature so feel free to add a feature request.
Edit: here's a test using declarative:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import class_mapper
Base= declarative_base()
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
type = Column(String)
__mapper_args__ = {"polymorphic_on": type, "polymorphic_identity": "a"}
class B(A):
pass
mapper = class_mapper(B)
mapper.polymorphic_identity = "b"
mapper.polymorphic_map["b"] = mapper
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
s.add_all([A(), B(), B(), A(), A()])
s.commit()
s.close()
assert [obj.type for obj in s.query(B).order_by(A.id)]
== ['b', 'b']
assert [type(obj) for obj in s.query(B).order_by(A.id)]
== [B, B]
assert [obj.type for obj in s.query(A).order_by(A.id)]
== ['a', 'b', 'b', 'a', 'a']
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…