I have an SQLAlchemy setup where one of my models is not stored in the database. The data comes off a web service instead and other models only store a primary key to refer to it.
How do I make this work while still using SQLAlchemy relationships? I'm caching the data locally in the database right now and updating from the web service when something changes, but this means I'm storing redundant copies of the data. I'd like to not store locally at all. Consider this:
Base = declarative_base()
# Web service-backed model
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
username = Column(Unicode(20), nullable=False)
@classmethod
def get(cls, id):
"""Get an instance using data from the web service."""
pass
# Local model
class Document(Base):
__tablename__ = 'document'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('user.id'))
user = relationship(User, backref='documents')
content = Column(UnicodeText)
I'd like to remove the User model from my database and retrieve it only from the web service (which is straightforward), while still preserving the benefits of a bi-directional relationship as provided by SQLAlchemy (Document.user and User.documents).
How do I achieve the latter?