Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are session methods unbound in sqlalchemy using sqlite?

Code replicating the error:

from sqlalchemy import create_engine, Table, Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class Message(Base):
    __tablename__ = 'messages'

    id = Column(Integer, primary_key=True)
    message = Column(Integer)


engine = create_engine('sqlite:///' + filename_of_your_choice)
session = sessionmaker(bind=engine)

newmessage = Message()
newmessage.message = "Hello"

messages = session.query(Message).all()

Running this code yields:

Traceback (most recent call last):
  File "C:/aaron/test.py", line 20, in <module>
    session.commit()
TypeError: unbound method commit() must be called with Session instance as first argument (got nothing instead)

I'm 95% positive that the filename isn't the issue as I can connect to it from the shell

any ideas?

like image 348
aaronasterling Avatar asked Jul 11 '10 04:07

aaronasterling


1 Answers

The return value from sessionmaker() is a class. You need to instantiate it before using methods on the instance.

like image 153
Ignacio Vazquez-Abrams Avatar answered Sep 19 '22 02:09

Ignacio Vazquez-Abrams