selecting literals fails with a union

Issue #1568 resolved
Former user created an issue

q1 = session.query(cls1, literal("cls1")) q2 = session.query(cls2, literal("cls2")) Q = q1.union(q2)

result is two subqueries that select the different literals, but the outer query also selects from a literal (passed as a seperate param) equal to the literal from q1.

Comments (4)

  1. Former user Account Deleted

    (in the (hopefully unlikely) case that this won't be fixed, a patch or suggested workaround would be greatly appreciated!)

  2. Mike Bayer repo owner

    there's a reason for that behavior (but I can't find the test for it, thought there was one). For a workaround:

    q1 = sess.query(cls1, literal("cls1").label('labelname'))
    q2 = ...
    
    u = union(q1, q2)
    sess.query(SomeClass, u.c.labelname).select_from(u)
    
  3. Mike Bayer repo owner

    OK yeah it was just something that was never figured out before. Have an approach that is better in 3984cad7228fcc8fae9c3be93ecd86f6b08ed4b3, takes the extra step of re-stating the columns in terms of the new union() construct instead of reusing those of the generating Query. this is against 0.6 since it is a behavioral change that can break some queries (such as if you queried literal_column("'x'") without a label).

  4. Log in to comment