On 01/18/2017 04:39 PM, univerio wrote:
If I have a mixed single/joined table inheritance hierarchy:
class Person(Base):
__tablename__ = "person"
id = Column(Integer, primary_key=True)
__mapper_args__ = {
"polymorphic_on": type,
}
class Employee(Person):
foo = Column(Integer)
__mapper_args__ = {
"polymorphic_identity": "employee",
}
class Contractor(Person):
__tablename__ = "contractor"
id = Column(Integer, ForeignKey(Person.id), primary_key=True)
__mapper_args__ = {
"polymorphic_identity": "contractor",
}
What do I put for "with_polymorphic" if I want only Employee (and not
Contractor) to be loaded?
with_polymorphic is really about what additional tables you want to load
up front, it's not about what kinds of objects will be "loaded". If a
row has "contractor" for polymorphic_identity you're going to get a
Contractor object back, it just wouldn't have the columns from
"contractor" loaded immediately until they are first accessed.
If you're trying to just query for Employee objects only, you can query
for that directly:
employees = session.query(Employee)
The only examples I could find are for
"with_polymorphic": "*".
In particular, 1. how do I get a reference to
Contractor if it's defined below Person (and it has to be below because
it inherits from Person), and 2. what's the second element in the tuple
supposed to be for a normal joined or single table inheritance scenario?
with_polymorphic at the mapper level was the original way this worked
and it was designed back before there was declarative, so you'd have
mapper() objects set up after you defined your classes. If you really
want to use with_polymorphic and want it at the mapper level with
declarative and specific classes, there's a
mapper._set_with_polymorphic() setter that does this use case for now.
It's semi-private only because this is not a common use case and the
accessor was added to work with the AbstractConcreteBase helper class.
Using it would look like:
Person.__mapper__._set_with_polymorphic(Employee)
--
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.