-2

I am trying to return the value of my SELECT query from my context manager. However, nothing is returned as a response. How can I return the results of my select query from my context manager/session?

@contextmanager
def aperio_session_scope():
  """Provide a transactional scope around a series of operations."""
  session = AperioSession()
  try:
    yield session
    session.commit()
  except:
    session.rollback()
    raise
  finally:
    session.close()

In addition, the query class looks like this:

class AperioSlidesAfterDate(object):
  def go(self, session):
    session.query(Slide).join(Image).filter(Image.scandate > '2018-08-01 00:00:00', Slide.barcodeid.isnot(None))

I run the query as follows:

  with aperio_session_scope() as session:
    slides = AperioSlidesAfterDate().go(session)

All 3 of these snippets are from different files and my imports are set up properly. No compile time or runtime exceptions. It seems that the value of slides is always None. Am I missing something? I followed the examples from SQLAlchemy docs.

If I do something like:

with aperio_session_scope() as session:
    slides = session.query(Slide).join(Image).filter(
        Image.scandate > '2018-08-01 00:00:00', Slide.barcodeid.isnot(None))

I get results, but I want to try an and use the session object with go as intended in the documentation. Is this a scoping issue? Or do I just need to somehow access the return value if at all possible?

Thanks.

RubyJ
  • 193
  • 2
  • 16

1 Answers1

1

You forgot to return the results of the query from go():

class AperioSlidesAfterDate(object):
  def go(self, session):
    return session.query(Slide).join(Image) ...
    # ^^^
jwodder
  • 54,758
  • 12
  • 108
  • 124
  • That was it! Silly me. I was too focused on implementing this new feature I forgot something simple. Thanks! – RubyJ Oct 17 '18 at 20:05