On Tue, Nov 5, 2019 at 11:05 PM Jonathan Vanasco <[email protected]> wrote: > > I'm not sure if something broke during an upgrade, or if I just had bad tests > and this was always an issue (I am assuming the latter!) > > I need to duplicate a relationship on a sqlalchemy ORM declarative object. > > I was hoping i could just do this (or at one point i could!) > > > class Foo(DeclaredTable): > bar_id = Column(Integer, ForeignKey("bar.id"), nullable=True) > bar = relationship("Bar", primaryjoin="Foo.bar_id ==Bar.id", > uselist=False) > bar_alt = bar > > > While I can address `fooInstance.bar_alt` with ease, I trigger an > AttributeError. The second item seems to become the 'active' attribute and > the first item is unaccessible. > > AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' > object associated with Foo.bar has an attribute '_supports_population' > > > There seems to be a workaround where I do this: > > class Foo(DeclaredTable): > bar_id = Column(Integer, ForeignKey("bar.id"), nullable=True) > bar = relationship("Bar", primaryjoin="Foo.bar_id ==Bar.id", > uselist=False) > > Foo.bar_alt = Foo.bar > > However, if there is a better way I would appreciate knowing it. I would > prefer to not monkeypatch the class like this for readability.
Isn't this what synonyms are for? https://docs.sqlalchemy.org/en/13/orm/mapped_attributes.html#synonyms Simon -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexeyLS7R44wPcRvuPvt2JD-%3DJoi7RrNb3USiP8cWiKv-ZA%40mail.gmail.com.
