I have a test file (attached below) with a base class called Test, and an
extended class called PolyTest, which is a polymorphic subclass of Test.
I want to query for Test and get back a full PolyTest object. I do not want
to use with_polymorphic: '*', as we have a large number of subclasses, only
one of which will ever be used at a time (when the fastcgi environ is set,
the subclass is assigned, and will not change for the life of the server).
Each subclass is a globally used thing, so for client A, every instance of
Test will always be TestA, which has that client's unique params. Using *
on with_poly generated a monstrous query spanning every subtable we have,
and is not an option.
So, given that PolyTest inherits from Test (implying test needs to be
mapped first), how do I pass the subclass PolyTest into the mapper_args of
Test? There is not a single example on the entire site which provides
guidance, as every single occurrence of with_polymorphic occurring within
mapper args is using '*'. This makes me wonder if it is even possible,
because I can't find a single example anywhere of someone passing classes
into mapper_args['with_poly'].
In the enclosed test, the final print should have a value attribute in it,
which it does not.
Also, using query.with_polymorphic is not a reasonable option here, as we
have a huge platform which expects to query() and get back the exact
result. We're not planning on replacing every instance of 'query()' in our
codebase.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sqlalchemy/-/l_78KhKtxogJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.
from sqlalchemy import *
from sqlalchemy import event
from sqlalchemy.ext.declarative import (
declared_attr, has_inherited_table, declarative_base)
from sqlalchemy.orm import Session
from sqlalchemy.orm.attributes import instance_dict
e = create_engine('sqlite:////tmp/test.db', echo=True)
Base = declarative_base()
Base.metadata = MetaData(e)
class Test(Base):
__tablename__ = 'tests'
@declared_attr
def __mapper_args__(cls):
if not has_inherited_table(cls):
ret = {
'polymorphic_identity': 'new',
'polymorphic_on': cls.type,
#'with_polymorphic': [PolyTest],
}
else:
ret = {'polymorphic_identity': cls.__name__}
return ret
id = Column(Integer, primary_key=True)
name = Column(String(50))
type = Column(String(32))
class PolyTest(Test):
__tablename__ = 'poly_tests'
id = Column(Integer, ForeignKey(Test.id), primary_key=True)
value = Column(Integer)
class PolyTest2(Test):
__tablename__ = 'poly_tests2'
id = Column(Integer, ForeignKey(Test.id), primary_key=True)
more_value = Column(Integer)
if __name__ == '__main__':
from pprint import pprint
Base.metadata.drop_all()
Base.metadata.create_all()
session = Session(e)
test = PolyTest(name='test', value=7)
session.add(test)
session.commit()
session.expunge_all()
test = session.query(Test).first()
print test
pprint(instance_dict(test))