开发者

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 nothin开发者_运维问答g instead)

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

any ideas?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜