orm_deannotate in columnprop broke yet another thing
Issue #2345
resolved
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import event
Base= declarative_base()
class Person(Base):
__tablename__ = "person"
id = Column(Integer, primary_key=True)
type = Column(String(20))
discriminator_expr = case(
["employee")]((type=='employee',),
else_="person"
)
discriminator = column_property(discriminator_expr)
__mapper_args__ = {
"polymorphic_on":discriminator_expr,
"polymorphic_identity":"person"
}
@event.listens_for(Person, "init", propagate=True)
def set_identity(instance, *arg, **kw):
"""Set the value of the 'type' column to a reasonable default."""
instance.type = instance.__mapper__.polymorphic_identity
class Employee(Person):
__tablename__ = 'employee'
id = Column(Integer, ForeignKey('person.id'), primary_key=True)
__mapper_args__ = {
"polymorphic_identity":"employee"
}
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
p1, p2, p3, p4 = Person(), Employee(), Person(), Person()
# set artificial types
p3.type = "engineer"
p4.type = "manager"
s.add_all([p2, p3, p4](p1,))
s.commit()
s.close()
r = [for x in s.query(Person).order_by(Person.id)](type(x))
assert \
r == \
[Employee, Person, Person](Person,)
Comments (5)
-
reporter -
reporter attached patch also implements most of
#2238, which we should consider closing as the functional approach might not be possible. -
reporter - changed status to resolved
that workaround plus many more changes / doc updates in d1cc7e7517da26521639fb22826d6e91e51c928e
-
reporter regression from this change fixed in 378e86124b598f2773cf124beab853ad63b71ee9, the conversion from
__clause_element__()
for declarative scenarios was lost. -
reporter - removed milestone
Removing milestone: 0.7.4 (automated comment)
- Log in to comment
a quick patch but I'd like to also get this to accommodate string property names and ColumnProperty objects directly