[sqlalchemy] Re: Consolidate multiple one-to-one into a list

2020-04-21 Thread Jonathan Vanasco
Assuming you are using declarative, this is okay (
https://docs.sqlalchemy.org/en/14/orm/mapping_api.html?#sqlalchemy.orm.Mapper
)

Note this line:
  A class which was mapped by the sqlalchemy.ext.declarative 

 extension will also have its mapper available via the __mapper__ attribute.

Mike generally doesn't change things once they're stated in the docs.

If you're not using declarative, it will probably be okay. Those things 
don't typically change.  If they do, you can easily grab the mapper in your 
property with something like this:

sqlalchemy.orm.class_mapper(self.__class__).relationships 

-- 
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/6a39b653-0139-4968-b6aa-c671769262cf%40googlegroups.com.


[sqlalchemy] Re: Consolidate multiple one-to-one into a list

2020-04-21 Thread Jens Troeger
So, here’s what I’m experimenting with and it seems to work:

@property
def children(self):
children = (getattr(self, r.key) for r in 
self.__mapper__.relationships if r.target.name == "child")
return [c for c in children if c is not None]

I’m not sure if this is the proper way to work with SQLA’s internals, but 
it gives me a list of children 邏

-- 
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/f9bc3585-d907-4058-8b87-31f6b56f3d9f%40googlegroups.com.