Jay Gattuso
Jay Gattuso

Reputation: 4130

Error with sessionmaker

I can see from this question Why are session methods unbound in sqlalchemy using sqlite? that I need to instantiate the class for sessionmaker.

I get the same error, and I assume the 'fix' will work, only I have no idea how to 'instantiate the class'

My set up code is as per the linked question.

From this question How to instantiate a class in python I can see that I need to call the class object, and my erroneous assumption is that the:

session = sessionmaker(bind=engine)

line is the instantiation.

Upvotes: 13

Views: 16650

Answers (2)

juliomalegria
juliomalegria

Reputation: 24911

Your problem is exactly the same as the first question you posted, and the solution should be the same as the selected answer from the same question.

The function sessionmaker returns a class, binding the engine passed in the bind parameter.

So, after creating the class, you have to instantiate it (haven't instantiate it yet):

Session = sessionmaker(bind=engine)
# Session is a class
session = Session()
# now session is a instance of the class Session
session.execute(...)

Upvotes: 33

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

That line creates the Session class. You still need to instantiate it.

Session = sessionmaker(bind=engine)
session = Session()

Upvotes: 7

Related Questions