the association proxies are in mapper.all_orm_descriptors if you can look in 
there

as far as "loaded" you have to dig into the proxy and the attribute it is 
proxying (which if you've done things this way can be a chain of proxies), I'd 
have to read the source to know the exact API to use :) see if the 
all_orm_descriptors gets you started



On Tue, Sep 24, 2019, at 6:46 PM, Jonathan Vanasco wrote:
> I recently shifted some of a database schema around, and thanks to 
> `association_proxy`, I've almost-eliminated the need to adjust my code 
> (thanks, Mike!). 
> 
> I'm left with a situation on my caching layer that I can't seem to work out. 
> I can't seem to to find out the following information (or other techniques 
> that can help me figure out what I need)
> 
> 1. what are the association_proxies on an object ?
> 2. have they been loaded ?
> 
> the use-case is this:
> 
> class Foo(base):
>  id = Column(Integer, primary_key=True)
> 
>  bar = relationship(
> "Bar",
>  primaryjoin="Foo.id==Bar.foo_id",
>  uselist=False,
> )
>  bar_id = association_proxy("bar", "id")
> 
> * I have a `Foo` instance, that may or may not have eagerloaded `Bar` into it.
> * I only want to access `bar_id` if `Bar` has been loaded.
> * I don't necessarily know that I have a `Foo` object or a `Bar`/`bar_id` 
> relation, as I am accessing this in a generic caching routing. I can 'hint' 
> this information into the object if needed.
> 
> 
> what I've tried/know:
> 
> * sqlalchemy.orm.class_mapper(Foo).mapped_table.c does not have `bar_id`
> * sqlalchemy.orm.class_mapper(Foo).relationships does not have `bar_id`; but 
> it does have `bar`
> * `fooInstance.__dict__` has a `bar` key when an eagerload happens
> 
> But i can't find a way to look at `fooInstance` and infer what the 
> association_proxies are.
> 
> My initial thought is to declare this stuff explicitly, but if there is a way 
> I could easily glean this information from the object, I think I may prefer 
> to do that.
> 
> does anyone have an idea?
> 
> 

> --
>  SQLAlchemy - 
>  The Python SQL Toolkit and Object Relational Mapper
> 
> http://www.sqlalchemy.org/
> 
>  To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
>  --- 
>  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 view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/322293a3-1605-45a9-abf8-c4ab29f3008f%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/322293a3-1605-45a9-abf8-c4ab29f3008f%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/ea52f82a-60f4-4168-85af-d0db34e96df8%40www.fastmail.com.

Reply via email to