6

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?

aaronasterling
  • 68,820
  • 20
  • 127
  • 125

1 Answers1

12

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358