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.
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()
Rolling Back. Session. rollback() rolls back the current transaction, if any.
session. flush() communicates a series of operations to the database (insert, update, delete). The database maintains them as pending operations in a transaction.
expunge sends a "persistent" object to the "detached" state. make_transient converts a "persistent" object to a "transient".
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(...)
That line creates the Session class. You still need to instantiate it.
Session = sessionmaker(bind=engine)
session = Session()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With