1

I've tried setting several options on my query such as:

session.query(CatalogueEntry)\
        .options(eagerload_all())\
        .filter(CatalogueEntry.source_path.in_(paths)).all()

But I keep getting errors:

...
File "/venv/lib/python2.7/site-packages/sqlalchemy/orm/__init__.py", line 245, in eagerload_all
return joinedload_all(*args, **kwargs)
File "/venv/lib/python2.7/site-packages/sqlalchemy/orm/strategy_options.py", line 665, in joinedload_all
_UnboundLoad.joinedload, keys, True, kw)
File "/venv/lib/python2.7/site-packages/sqlalchemy/orm/strategy_options.py", line 315, in _from_keys
opt = meth(opt, all_tokens[-1], **kw)
IndexError: list index out of range

If I change this to:

session.query(CatalogueEntry)\
        .options(eagerload_all('*'))\
        .filter(CatalogueEntry.source_path.in_(paths)).all()

I get:

DetachedInstanceError: Instance <CatalogueEntry at 0x1026936d0> is not bound to a Session; attribute refresh operation cannot proceed

I want to detach my objects from the session and pass them back into client code that will only read the properties. Before I added options I was getting DetachedInstanceErrors.

How can I return fully hydrated objects from an sqlalchemy (0.9.4) query whose properties can be read by other parts of my code after the session is closed?

1 Answer 1

4
session.query(CatalogueEntry)\
        .options(joinedload('name_of_relationship_attribute'))\
        .filter(CatalogueEntry.source_path.in_(paths)).all()

There is also subqueryload

You can also set the default load method in the relationship definition with lazy='joined' or lazy = 'subquery'

see this url for reference http://docs.sqlalchemy.org/en/rel_0_9/orm/loading.html?highlight=joinedload#sqlalchemy.orm.joinedload_all

Sign up to request clarification or add additional context in comments.

2 Comments

The thing is, there aren't any relationships at all. CatalogueEntry represents a single, standalone table. I just want the objects fully hydrated so I can use them outside the session. Will this still work?
I am not sure I follow what you mean then by fully hydrated. If there are no relationships there is nothing to eager load as the definition of eager loading Is to load additional relationships at the time you query the main table. There is another set of options is SQLAlchemy that allow you to control which columns of a table are loaded when you query a model, but by default you will get all the columns in your example unless you have specified otherwise in your model definition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.