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

2020-04-24 Thread Jonathan Vanasco
On Friday, April 24, 2020 at 1:16:10 PM UTC-4, Jens Troeger wrote: > > > If I understand you correctly, then the solution above is as good as it > gets and SQLA doesn’t provide a builtin solution for what I’m trying to do? > There are so many hooks in SqlAlchemy, there may still be a more

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

2020-04-24 Thread Jens Troeger
Thanks Jonathan! Yes, all classes derive from the declarative base: from sqlalchemy.ext.declarative import declarative_base Base = declarative_base(metadata=MetaData(naming_convention={...})) class Parent(Base): ... class Child(Base): ... If I understand you correctly, then the

[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

[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

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

2020-04-18 Thread Jens Troeger
Thank you, Jonathan. I’ve used SQLA’s association proxies before, I’ll take a look again. You bring up a good point, though: Ok, so this isn't a one-to-one relationship, but a many-to-many > relationship. > That’s something I’ve been debating with myself for a while before I posted here: the

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

2020-04-18 Thread Jonathan Vanasco
On Friday, April 17, 2020 at 8:02:50 PM UTC-4, Jens Troeger wrote: > > > Indeed, Child does have multiple parents… > Ok, so this isn't a one-to-one relationship, but a many-to-many relationship. I would opt for a 3 table structure: Parent Parent_2_Child Child The

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

2020-04-17 Thread Jens Troeger
Hi Jonathan, The line you commented out from the example was either: > > children = relationship("Child") > > children = relationship("Child", back_populates="parent") > > > both of those lines create an iterable list of all the Child objects on > the `children` > Neither of them would work,

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

2020-04-14 Thread Jonathan Vanasco
This departs a bit from the example, because you are caching the youngest and oldest ids onto the Parent object. is that necessary for your usage? > Now my question is: how can I introduce a set/list of all children on the parent? The line you commented out from the example was either: