Ajwadd Anwarr
Ajwadd Anwarr

Reputation: 21

Session_scope raises attribute error when using Sqlalchemy

I have tried to implement the session scope from the sqlalchemy session documents but for some reason I am getting a attributeerror:exit at with session_scope as session:

The code looks like this:

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



class SearchPhoneTable():
 def DateSearch(self,session,StartDate,EndDate):
    DateValue=[]
    OrderValue=[]
    SessionResult=session.query(VideoChange).filter(and_(VideoChange.Date <= EndDate, VideoChange.Date>=StartDate)).all()
    for elements in SessionResult:
        DateValue.append(elements.Date)
        OrderValue.append(elements.ViewsValue)
    return DateValue,OrderValue


def PDFOnline2(StartDate,EndDate):

 with session_scope as session:
  Xaxis,Yaxis=SearchPhoneTable().DateSearch(session,StartDate,EndDate)


 for elements in Yaxis:
    print(elements)

 for elements in Xaxis:
    print(elements)


 return 0

Upvotes: 1

Views: 511

Answers (1)

Adam Byrtek
Adam Byrtek

Reputation: 12202

You're very close, you just need to call the context manager function:

with session_scope() as session:

Notice the parentheses.

Upvotes: 1

Related Questions