Thanks for the quick reply Michael. I would love to hear what you can find 
out about it.

I have what I think is a pretty horrible and dirty “solution”. But it 
*seems* to work. What I’ve done is to assign the key attribute of columns 
like this:

from collections import namedtuple

MyBundleAttr = namedtuple('MyBundleAttr', ['attr', 'key'])
class MyBundle(Bundle):

    def __init__(self, name, *exprs, **kw):
        new_exprs = []
        for expr in exprs:
            # Support custom Bundle attr keys. I don't know the side-effects...
            if isinstance(expr, MyBundleAttr):
                new_expr = expr.attr
                new_expr.key = expr.key
            else:
                new_expr = expr
            new_exprs.append(new_expr)
        super().__init__(name, *new_exprs, **kw)

And then I use the newly created MyBundleAttr like this:

primate_bundle = MyBundle(
    'primate',
    Primate.name,
    MyBundle('wooden_tool', *[
        WoodenTool.id,
        WoodenTool.name,
        MyBundleAttr(WoodenToolCategory.name, 'category'),
    ]),
    MyBundle('solid_tool', *[
        SolidTool.id,
        SolidTool.name,
        MyBundleAttr(SolidToolCategory.name, 'category'),
    ]),
    single_entity=True,
)

I'm guessing this could cause all kinds of side-effects... Right?

On Thursday, April 9, 2015 at 8:29:15 PM 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.
>  
> Yeah, OK I see why that is, I'll try to take a look at this later today.  
> The Bundle thing is obviously new and you're the first person I'm seeing 
> actually use it.    You might need to work around for now :/
>
>
>
>
>
>   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 sqlalchemy+...@googlegroups.com <javascript:>.
> To post to this group, send email to sqlal...@googlegroups.com 
> <javascript:>.
> 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to