Hi!
I don't even quote my old message since it's just confusing.
In my head the question made sense ;)
So I try again with a code example:
I have a class `User`:
class User(Base): id = Column(Integer, primary_key=True) name = Column(String(64)) email = Column(String(64)) languages = relationship('Language', secondary='user_languages')
I already have a lot of users stored in my DB.
And I know that I have, for example, this user in my DB:
user_dict = { 'id': 23, 'name': 'foo', 'email': 'foo@bar', }
So I have all the attributes but the relations.
Now I want to make a sqlalchemy `User` instance
and kind of register it in sqlalchemy's system
so I can get the `languages` if needed.
user = User(**user_dict) # Now I can access the id, name email attributes assert user.id == 23 # but since sqlalchemy thinks it's a new model it doesn't # lazy load any relationships assert len(user.languages) == 0 # I want here that the languages for the user with id 23 appear # So I want that `user` is the same as when I would have done user_from_db = DBSession.query(User).get(23) assert user == user_from_db
The use-case is that I have a big model with lots of complex
relationships but 90% of the time I don't need the data from those.
So I only want to cache the direct attributes plus what else I need
and then load those from the cache like above and be able to
use the sqlalchemy model like I would have queried it from the db.
Hope it makes a bit more sense now.
Sorry for the confusing first question and wasting your time.
Thanks,
Daniel
The use-case is that I have a big model with lots of complex
relationships but 90% of the time I don't need the data from those.
> If I'm reading your question correctly, most of what sqlalchemy does (and
> excels at) is specifically keeping people from doing what you're trying to
> do.
I think you got me wrong then.
> It seems like you're trying to avoid all the work that is done to
> ensure data integrity across sessions and transactions. (Which is a common
> need)
Nope,
I just want to use a cache where I only store only the DB row for my
model and not all data from relation tables as well.