I have two classes where one f-keys onto another.
Things work perfectly:
class Foo(Base):
id = Column(Integer, primary_key=True)
bar_id = Column(Integer, ForeignKey("bar.id"), nullable=True)
bar = relationship(
"bar",
primaryjoin="Foo.bar_id==Bar.id",
uselist=False,
back_populates="foo",
)
class Bar(Base):
id = Column(Integer, primary_key=True)
foo = relationship(
"Foo",
primaryjoin="Bar.id==Foo.bar_id",
uselist=False,
back_populates="bar",
)
Thanks to SQLAlchemy, I can do this:
myFoo.bar = myBar
As expected `myFoo.bar_id` is updated. Wonderful.
I am working on some new functionality, and hit a potential performance
issue. While my `Foo` objects inherently know if there is an associated
`Bar`, I have to query (direct or via lazy-load) the database to find out
if a `Bar` has an associate `Foo`. In most situations, this is not an
issue. In a few contexts, the lazyloading or joins are a bit burdonsome.
Is there a way to set up the `relationship` so I could do cache the
`foo_id` on Bar? Something like this:
class Foo(Base):
id = Column(Integer, primary_key=True)
bar_id = Column(Integer, ForeignKey("bar.id"), nullable=True)
bar = relationship("Bar", ???)
class Bar(Base):
id = Column(Integer, primary_key=True)
foo_id = Column(Integer, ForeignKey("foo.id"), nullable=True)
foo = relationship("Foo", ???)
This is obviously an anti-pattern in database design, as I only need to
duplicate this data to improve performance in a few places.
--
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/171b01dc-787e-43b6-bd8c-afdceb211543%40googlegroups.com.