On 9/8/15 4:13 AM, Jacob Magnusson wrote:
Michael,

Is there any chance we might see support for /with_labels/ in this use case? Even though my "solution" works, it's somewhat of an annoyance with all these warnings being spit out:

    /SAWarning: Column 'id' on table <sqlalchemy.sql.selectable.Select
    at 0x110cd5d30; Select object> being replaced by Column('id',
    Integer(), table=<Select object>, primary_key=True,
    nullable=False), which has the same key.  Consider use_labels for
    select() statements./


It's not clear how with_labels() can be made to work with this without a lot of re-thinking of many of the internal aspects of the Query and related objects. OTOH, the Bundle object is fully customizable and as I recommended, a custom approach that applies labels to Column objects as they are passed and translates back as needed on the result side is feasible right now; I'd rather add a labeling/nesting feature to Bundle itself based on the discoveries gained by working with that approach as a recipe.






On Friday, April 10, 2015 at 12:19:31 AM UTC+2, Michael Bayer wrote:



    On 4/9/15 1:50 PM, Jacob Magnusson wrote:

    I have this case with a bundle that looks something like this:

    |primate_bundle = Bundle( 'primate', Primate.name,
    Bundle('wooden_tool', *[ WoodenTool.id, WoodenTool.name,
    WoodenToolCategory.name.label('category'), ]),
    Bundle('solid_tool', *[ SolidTool.id, SolidTool.name,
    SolidToolCategory.name.label('category'), ]) ) |

    Then I query it like this:

    |session.query(primate_bundle) .select_from(Primate)
    .join(WoodenTool, Primate.main_tool) .join(WoodenToolCategory,
    WoodenTool.category_id == WoodenToolCategory.id) .join(SolidTool,
    Primate.secondary_tool) .join(SolidToolCategory,
    SolidTool.category_id == SolidToolCategory.id) .all() |

    However, since the label for category name is the same within
    both sub-bundles it will throw |Ambiguous column name| (because
    the compiled SQL labels will be exactly the same). Adding
    |.with_labels()| doesn’t fix it. Full traceback can be seen by
    running the included examples. Commenting out one of the
    |.label()| lines in the example makes it runnable. Do you guys
    have a clean solution to support this use case? I really like
    this feature of creating your own custom made results so it would
    be a shame to not be able to do this.

    OK well here's one hacky way, that we could better accommodate by
    adding a "key" parameter to label(), this is sort of like your
    other approach: ay = A.y.label(None) ay.key = 'foobar' bp =
B.p.label(None) bp.key = 'foobar' ab = Bundle( 'ab', Bundle('a', *[ A.id, A.x, ay, ]), Bundle('b', *[ B.id, B.q, bp, ]) ) The
    other way is to provide create_row_processor() as we describe
    here:
    
http://docs.sqlalchemy.org/en/rel_0_9/orm/loading_columns.html?highlight=bundle#column-bundles
    
<http://docs.sqlalchemy.org/en/rel_0_9/orm/loading_columns.html?highlight=bundle#column-bundles>.
    That way you could just make this work any way you wanted: from
    sqlalchemy import util class LabelBundle(Bundle):     def
    __init__(self, *arg, **kw):         self.extra_labels =
    kw.pop('extra_labels', {})         super(LabelBundle,
    self).__init__(*arg, **kw)     def create_row_processor(self,
query, procs, labels): # or use a NamedTuple here keyed_tuple = util.lightweight_named_tuple( 'result',
    [self.extra_labels.get(l, l) for l in labels])         def
    proc(row):             return keyed_tuple([proc(row) for proc in
procs]) return proc ab = LabelBundle( 'ab', LabelBundle( 'a', extra_labels={'y': 'foobar'}, *[A.id, A.x, A.y]), LabelBundle( 'b', extra_labels={'p': 'foobar'}, *[B.id, B.q, B.p]) )

    Tested on SQLAlchemy 1.0.0b5 and 0.9.9. Python 3.

    Thank you so much for any potential help you can give me on this.
    I’ve followed the source code for |Bundle| but I can’t think of a
    clean way to this…

    ​
    -- 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] <javascript:>. To post to this
    group, send email to [email protected] <javascript:>.
    Visit this group at http://groups.google.com/group/sqlalchemy
    <http://groups.google.com/group/sqlalchemy>. For more options,
    visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.

-- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.

--
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to