Neo
Neo

Reputation: 13881

sqlalchemy: Error with commit on session - 'SessionMaker' object has no attribute '_model_changes'

I'm new to SqlAlchemy. We were working primarily with Flask, but in a particular case I needed a manual database connection. So I launched a new db connection with something like this:

write_engine = create_engine("mysql://user:pass@localhost/db?charset=utf8")
write_session = scoped_session(sessionmaker(autocommit=False,
    autoflush=False,bind=write_engine))

nlabel = write_session.query(Label).filter(Label.id==label.id).first() # Works
#Later in code
ms = Message("Some message")
write_session.add(ms) # Works fine
write_session.commit() # Errors out

Error looks like "AttributeError: 'SessionMaker' object has no attribute '_model_changes'"

What am I doing wrong?

Upvotes: 3

Views: 4950

Answers (2)

user2341072
user2341072

Reputation: 31

Your issue is that you are missing this line:

db_session._model_changes = {}

Upvotes: 2

bm1362
bm1362

Reputation: 41

From the documentation I think you might be missing the initialization of the Session object.

Try:

Session = scoped_session(sessionmaker(autocommit=False, autoflush=False,bind=write_engine))
write_session = Session()

It's a shot in the dark- I'm not intimately familiar with SQLAlchemy. Best of luck!

Upvotes: 3

Related Questions