difference in label with first() or db.session.execute().first()

Issue #3506 resolved
Sandeep Srinivasa created an issue

we have a fairly complex query with inner and outer joins and aliases.

we normally use the following syntax

q = db.session.query(UserCarts,Merchant,Organization).join(Merchant,Merchant.c.id == UserCarts.c.merchant_id).outerjoin(Organization,Organization.c.id == Merchant.c.organization_id).filter(db.and_(UserCarts.c.cart_id == '0f05b5e25ce946af9e0a3b37f0843870'))

if we use

q.first().keys()

, we get repeated keys "name" between Merchant and Organization. I tried this with "with_labels()" at the end as well.

however if I do this:

p = db.session.execute(q)
s = p.first()

in that case, I get labeled keys. Why is there a difference ?

Comments (14)

  1. Mike Bayer repo owner

    can you please illustrate mappings succinctly as well as what the specific keys you are getting are, thanks.

  2. Sandeep Srinivasa reporter

    this is used from within flask

        metadata = db.MetaData()
        metadata.reflect(bind = db.get_engine(app))
        User = metadata.tables['users']
        MerchantIdentity = metadata.tables['merchant_identities']
        UserCarts = metadata.tables['user_carts']
        Merchant = metadata.tables['merchants']
        Relationship = metadata.tables['relationships']
        TransactionLog = metadata.tables['transaction_logs']
        PhoneIdentity = metadata.tables['phone_identities']
    
        org_user_alias = db.aliased(User, name = "org_user")
        merchant_user_alias = db.aliased(User, name = "merchant_user")
    

    I'm using reflection to work with an existing database.

  3. Mike Bayer repo owner

    those are Table objects. The ResultProxy you get back from session.execute() is going to have much richer behavior here; if you want to build up using Query, you might want to say session.execute(query.with_labels().statement).

  4. Mike Bayer repo owner

    hmm even query.with_labels() won't affect those column labels. Just use session.execute() with Core, that is, use the select() object. It's much better when dealing with only straight tables.

  5. Sandeep Srinivasa reporter

    no problem - in fact with_labels() has no impact on the result. The same two different results come up with or without with_labels().

    it would be nice if you could qualify that in the documentation. I was not aware that I should be expecting a difference in richness even though I use with_labels.

  6. Mike Bayer repo owner

    here's the docs for "with_labels()":

            """Apply column labels to the return value of Query.statement.
    
            Indicates that this Query's `statement` accessor should return
            a SELECT statement that applies labels to all columns in the
            form <tablename>_<columnname>; this is commonly used to
            disambiguate columns from multiple tables which have the same
            name.
    
            When the `Query` actually issues SQL to load rows, it always
            uses column labeling.
    

    it's pretty clear this applies to only the ".statement" accessor, no?

  7. Log in to comment