it should. is child2 a subclass of child1 ?
So to answer your question, with the configuration given, there's no
way for the polymorphic load to return only Child1 objects if the
corresponding SQL is returning other kinds of rows. As an
alternative you can create a non-primary mapper that is only against
"parent join child1" (i.e. mapper(Child1, parent.join(child1),
non_primary=True) ) and query using that mapper - but that will still
interpret rows which correspond to Parent or Child2 objects as Child1
objects, which is "incorrect" by most standards. If OTOH you really
want just the rows that have "child1" as a type, and for some reason
you can't establish that criteria in the SQL, then you'd need to
filter the results after the fact.
though I can't imagine a scenario where an existing SQL statement is
present that cannot have a simple "WHERE type='child1'" added to it.
>
> At the moment i use a workaround which does exactly that what you
> mentioned. It is a 'wanted class' query string discriminator.
> With the example this would work properly. The only thing i do not
> know is how to get the 'polymorphic_identity' which is defined in the
> mapper. How can i retrieve this String when only the class is given?
you could say "class_mapper(MyClass).polymorphic_identity".
>
> But there is still my bigger problem, of course we have to modify the
> scenario:
>
> class Parent(object):
> class Child(Parent):
> class SubChild1(Child):
> class SubChild2(Child):
>
> Now i want to get all Child objects, this means all Child, Subchild1
> and Subchild2 objects.
>
> result = session.query(Child).from_statement(stmt).all()
>
> The parent class of Subchild1 and Subchild2 - Child does not know
> anything about any subclasses.
>
> So if i only have the Child class i can not imagine a smart way to
> solve this, because in plain SQL i need all possible type values.
> The nice thing about SQLAlchemy was the automatic inheritance
> resolver.
the Query() object, if you were not using from_statement(), would
generate SQL that joins from the parent to child table and returns all
rows. This returns for you all Child, SubChild1, and SubChild2
objects. parent rows which are not child rows wont be returned since
they do not join to child. The remaining columns from subchild1 and
subchild2 are loaded in separate SELECT statements.
If OTOH you were using single table inheritance, the Query would in
fact produce a clause like parent.type.in_('child', 'subchild1',
'subchild2').
But for joined table inheritance, the join you're looking for here is:
select * from parent JOIN child on parent.id=child.id
that way you also don't need to know the discriminator.
>
> Ok, the real world structure was a little bit more comnplicated, but
> it worked out this way.
> There is only one problem left, regarding the the name of the FK
> column which is responsible for resolving the inheritance.
> For constructing my join condition, i need to know the name of that
> column. Sometimes it is id, sometimes it is for example foo_id.
>
> I searched with
>
> print dir(class_mapper(MyClass).mapped_table)
> print dir(class_mapper(MyClass).local_table)
>
> and so on...
> I did not find a way to resolve the name of the column which is
> responsible for inheritance.
you can get the full join condition (the onclause) with
class_mapper(MyClass).inherit_condition. You can also just join the
tables in question together using table1.join(table2) and if the
foreign key relation between them is unambiguous, the onclause is
generated automatically (which is the same way inherit_condition is
determined).