query() should interpret a many-to-one relation() as an entity?

Issue #1328 resolved
Former user created an issue

The following query:

list(session.query(Employee, Employee.company))

unexpectedly returns 5-element tuples

[e1, 1, u'e1', u'employee', 1),
 (Employee e2, 2, u'e2', u'employee', 1),
 (Employee e3, 3, u'e3', u'employee', 2)]((Employee)

IMHO no one expects such a result.

Full test case attached.

Comments (11)

  1. Mike Bayer repo owner

    this is an enhancement, that you'd like query() to intelligently interpret a relation().

    But the documented way to do this is:

    query(Employee, Company)
    

    additionally its unclear if query(Employee, Employee.company) should automatically "join" the two tables together. Once again, there is a clear method for this already:

    query(Employee, Company).join(Employee.company)
    

    So I think the best behavior here would be for query(some_relation()) to just raise an error.

  2. Mike Bayer repo owner

    lately I've been swinging the other way on this. if you query(Parent.child), it really is obvious what you're looking for. just wondering about implementation.

  3. Former user Account Deleted

    Here's a patch that adds some tests and an implementation. It touches a fair amount of code, but a lot of that is just to add a simple way for queries to join themselves.

    Also, the only tests this has are the basic query(Parent.child) case and the add_entity(Parent.child) case. Are there others?

    Final thought: should this work for one-to-many relations as well? e.g. query(Child.parents)

    -Scott Torborg

  4. Log in to comment