Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 643
Jay Gattuso Avatar asked Apr 22 '12 00:04

Jay Gattuso


People also ask

What is Sessionmaker in Python?

Session class is defined using sessionmaker() – a configurable session factory method which is bound to the engine object created earlier. from sqlalchemy. orm import sessionmaker Session = sessionmaker(bind = engine) The session object is then set up using its default constructor as follows − session = Session()

What is Session rollback?

Rolling Back. Session. rollback() rolls back the current transaction, if any.

What is Session flush in Sqlalchemy?

session. flush() communicates a series of operations to the database (insert, update, delete). The database maintains them as pending operations in a transaction.

What is Make_transient?

expunge sends a "persistent" object to the "detached" state. make_transient converts a "persistent" object to a "transient".


2 Answers

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(...)
like image 60
juliomalegria Avatar answered Sep 18 '22 06:09

juliomalegria


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

Session = sessionmaker(bind=engine)
session = Session()
like image 43
Ignacio Vazquez-Abrams Avatar answered Sep 21 '22 06:09

Ignacio Vazquez-Abrams